Last active
May 5, 2020 12:50
-
-
Save alexeygrigorev/4146b2bfed944977a9abd8988d9be604 to your computer and use it in GitHub Desktop.
A function for parallel map with tqdm
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
import multiprocessing | |
from concurrent.futures import ProcessPoolExecutor | |
from tqdm import tqdm | |
num_cores = multiprocessing.cpu_count() | |
pool = ProcessPoolExecutor(max_workers=num_cores) | |
def map_progress(pool, seq, f): | |
results = [] | |
with tqdm(total=len(seq)) as progress: | |
futures = [] | |
for el in seq: | |
future = pool.submit(f, el) | |
future.add_done_callback(lambda p: progress.update()) | |
futures.append(future) | |
for future in futures: | |
result = future.result() | |
results.append(result) | |
return results | |
# use: | |
# map_progress(pool, messages, anonymize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment