Skip to content

Instantly share code, notes, and snippets.

@alexeygrigorev
Last active May 5, 2020 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeygrigorev/4146b2bfed944977a9abd8988d9be604 to your computer and use it in GitHub Desktop.
Save alexeygrigorev/4146b2bfed944977a9abd8988d9be604 to your computer and use it in GitHub Desktop.
A function for parallel map with tqdm
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