Ult*_*eld 5 python multiprocessing tqdm
由于我的工作函数中需要多个参数,所以我使用了星图,但是如何使用 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()
Run Code Online (Sandbox Code Playgroud)
那么如何使用 tqdm 来显示进度?
Sra*_*raw -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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3691 次 |
| 最近记录: |