Last active
December 13, 2019 13:58
-
-
Save PiotrDabrowskey/1784f50f6bd70cceaaf890f1b99bf453 to your computer and use it in GitHub Desktop.
python multiprocess tqdm example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocess import Pool | |
def tqdm_multiprocessing_map(fun, sequence, unordered=False, unpack_args=False, debug=False): | |
if unpack_args: | |
def fun_wrapper(args): | |
return fun(*args) | |
else: | |
fun_wrapper = fun | |
try: | |
total = len(sequence) | |
except TypeError: | |
sequence = list(sequence) | |
total = len(sequence) | |
if debug: | |
return list(map(fun_wrapper, sequence)) | |
with Pool() as pool: | |
map_fun = pool.imap_unordered if unordered else pool.imap | |
sequence = map_fun(fun_wrapper, sequence) | |
sequence = tqdm(sequence, total=total, leave=False) | |
return list(sequence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment