Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we use tqdm in a parallel execution with joblib?

I want to run a function in parallel, and wait until all parallel nodes are done, using joblib. Like in the example:

from math import sqrt from joblib import Parallel, delayed Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10)) 

But, I want that the execution will be seen in a single progressbar like with tqdm, showing how many jobs has been completed.

How would you do that?

like image 238
Dror Hilman Avatar asked Jun 14 '16 06:06

Dror Hilman


People also ask

How does Joblib parallel work?

Parallel provides a special handling for large arrays to automatically dump them on the filesystem and pass a reference to the worker to open them as memory map on that file using the numpy. memmap subclass of numpy. ndarray . This makes it possible to share a segment of data between all the worker processes.

Can you use tqdm with multiprocessing?

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

Does Joblib parallel preserve order?

TL;DR - it preserves order for both backends.

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.


2 Answers

Just put range(10) inside tqdm(...)! It probably seemed too good to be true for you, but it really works (on my machine):

from math import sqrt from joblib import Parallel, delayed   from tqdm import tqdm   result = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in tqdm(range(100000))) 
like image 150
tyrex Avatar answered Sep 30 '22 18:09

tyrex


I've created pqdm a parallel tqdm wrapper with concurrent futures to comfortably get this done, give it a try!

To install

pip install pqdm 

and use

from pqdm.processes import pqdm # If you want threads instead: # from pqdm.threads import pqdm  args = [1, 2, 3, 4, 5] # args = range(1,6) would also work  def square(a):     return a*a  result = pqdm(args, square, n_jobs=2) 
like image 37
niedakh Avatar answered Sep 30 '22 18:09

niedakh