From c4506558f552e96dcd893483c3ad92829a363961 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Mon, 18 Feb 2019 22:19:41 -0800 Subject: [PATCH] Added par_map utility --- beets/util/__init__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 69870edf2..ba7393975 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -24,6 +24,7 @@ import re import shutil import fnmatch from collections import Counter +from multiprocessing.pool import ThreadPool import traceback import subprocess import platform @@ -1009,3 +1010,22 @@ def asciify_path(path, sep_replace): sep_replace ) return os.sep.join(path_components) + + +def par_map(transform, items): + """ + This module implements a simple utility to either: + a) Perform a parallel map when running under Python >=3 + b) Perform a sequential map otherwise + + This is useful whenever there is some operation `do_something()` which we + want to efficiently apply to our music library. + """ + if sys.version_info[0] < 3: + for item in items: + transform(item) + else: + pool = ThreadPool() + pool.map(transform, items) + pool.close() + pool.join()