繁体   English   中英

python多处理starmap进度条

[英]python multiprocessing starmap progressbar

由于我在worker函数中需要几个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呢?

您应该创建一个进程来监视其他进程传递的信号并更新您的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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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