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

后端 未结 6 800
轮回少年
轮回少年 2021-02-05 01:25

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 Par         


        
6条回答
  •  Happy的楠姐
    2021-02-05 02:01

    As noted above, solutions that simply wrap the iterable passed to joblib.Parallel() do not truly monitor the progress of execution. Instead, I suggest subclassing Parallel and overriding the print_progress() method, as follows:

    import joblib
    from tqdm.auto import tqdm
    
    class ProgressParallel(joblib.Parallel):
        def __call__(self, *args, **kwargs):
            with tqdm() as self._pbar:
                return joblib.Parallel.__call__(self, *args, **kwargs)
    
        def print_progress(self):
            self._pbar.total = self.n_dispatched_tasks
            self._pbar.n = self.n_completed_tasks
            self._pbar.refresh()
    

提交回复
热议问题