Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use tqdm with concurrent.futures?

I have a multithreaded function that I would like a status bar for using tqdm. Is there an easy way to show a status bar with ThreadPoolExecutor? It is the parallelization part that is confusing me.

import concurrent.futures  def f(x):     return f**2  my_iter = range(1000000)  def run(f,my_iter):     with concurrent.futures.ThreadPoolExecutor() as executor:         function = list(executor.map(f, my_iter))     return results  run(f, my_iter) # wrap tqdr around this function? 
like image 344
max Avatar asked Jul 30 '18 20:07

max


People also ask

Can you use tqdm with multiprocessing?

tqdm(range(0, 30)) does not work with multiprocessing (as formulated in the code below).

Can I use tqdm in while loop?

tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!

What is Python concurrent futures?

The concurrent. futures module provides a high-level interface for asynchronously executing callables. The asynchronous execution can be performed with threads, using ThreadPoolExecutor , or separate processes, using ProcessPoolExecutor .

Is tqdm only for loops?

I think tqdm is meant for long loops, not short loops that takes a lot of time. That is because tqdm estimates the ETA based on the average time it took a cycle to complete, so it wont be that useful.


1 Answers

You can wrap tqdm around the executor as the following to track the progress:

list(tqdm(executor.map(f, iter), total=len(iter))

Here is your example:

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):     with concurrent.futures.ThreadPoolExecutor() as executor:         results = list(tqdm(executor.map(f, my_iter), total=len(my_iter)))     return results  my_iter = range(100000) run(f, my_iter) 

And the result is like this:

16%|██▏           | 15707/100000 [00:00<00:02, 31312.54it/s] 
like image 67
Dat Avatar answered Sep 17 '22 05:09

Dat