Skip to content

Instantly share code, notes, and snippets.

@ltalirz
Created July 20, 2021 16:23
Show Gist options
  • Select an option

  • Save ltalirz/9220946c5c9fd920a1a2d81ce7375c47 to your computer and use it in GitHub Desktop.

Select an option

Save ltalirz/9220946c5c9fd920a1a2d81ce7375c47 to your computer and use it in GitHub Desktop.
tqdm with ProcessPoolExecutor
import time
import concurrent.futures
from tqdm import tqdm
def f(x):
time.sleep(0.001) # to visualize the progress
return x**2
def run(f, my_iter):
l = len(my_iter)
with tqdm(total=l) as pbar:
# let's give it some more threads:
with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(f, arg): arg for arg in my_iter}
results = {}
for future in concurrent.futures.as_completed(futures):
arg = futures[future]
results[arg] = future.result()
pbar.update(1)
print(321, results[321])
if __name__ == '__main__':
my_iter = range(100000)
run(f, my_iter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment