简体   繁体   中英

tqdm progress bar and multiprocessing

I'm trying to add a progression bar to my program, however, solutions that seems to works for other (on other posts) do not work for me.

Python version 3.6.

import multiprocessing as mp
import tqdm

def f(dynamic, fix1, fix2):
    return dynamic + fix1 + fix2

N = 2

fix1 = 5
fix2= 10

dynamic = range(10)

p = mp.Pool(processes = N)

for _ in tqdm.tqdm(p.starmap(f, [(d, fix1, fix2) for d in dynamic]), total = len(dynamic)):
    pass

p.close()
p.join()

Any idea why the multiprocessing works (the computation is done), but there is no progress bar?

NB: The example above is dummy, my function are different.

Other question: how can I interrupt properly a multiprocessing program? The ctrl + C that I usually do in signle thread seems to pose some issues.

Unfortunately, tqdm is not working with starmap. You can use the following:

def f(args):
  arg1, arg2 = args
  ... do something with arg1, arg2 ...


for _ in tqdm.tqdm(pool.imap_unordered(f, zip(list_of_args, list_of_args2)), total=total):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM