Added par_map utility

This commit is contained in:
Bernardo Meurer 2019-02-18 22:19:41 -08:00
parent 81c5ae3fdf
commit c4506558f5
No known key found for this signature in database
GPG key ID: E421C74191EA186C

View file

@ -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()