由于我的工作函数中需要多个 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 来显示进度?
最佳答案
您应该创建一个进程来监视其他进程传递的信号并更新您的 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.
关于python多处理星图进度条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43714126/