5

由于我的工作函数中需要几个 args,所以我使用了 starmap,但是如何使用 tqdm 显示进度?

from itertools import repeat
from multiprocessing import Pool

def func(i, a, b, c):
    print(i, a, b, c)

if __name__ == '__main__':  
    pool = Pool(thread_num)
    a, b, c = 'a', 'b', 'c'
    pool.starmap(func, zip(range(100), repeat(a), repeat(b), repeat(c)))
    pool.close()
    pool.join()

那么如何使用 tqdm 来显示 pregress 呢?

4

1 回答 1

-1

您应该创建一个进程来监控其他进程传递的信号并更新您的 tqdm。给你一个最小的例子:

from multiprocessing import Queue, Pool, Process

def listener(q, num):
    tbar = tdqm(total = num)
    for i in iter(q.get, None):
        tbar.update()
    tbar.close()

def worker(q):
    do something.
    queue.put(1)

if __name__ == "__main__":
    ....
    q.put(None)  #when you finish your work, put a None signal to finish the listener.
于 2017-05-01T05:31:37.023 回答