Python进程池中实现进度条显示

时间:2020-07-20 22:04:45   收藏:0   阅读:141

Python进程池中实现进度条显示

  今天使用进程池爬虫,爬的网页太多,想通过进度条来显示出来,但是发现并没有想象的那么简单。

  Python中多进程使用Queue来数据共享,进程池使用Manager().Queue()来实现数据共享,如果想使用进程回调函数,则进程函数一定要返回参数。

  最后在github一段下面找到解决代码,如下:

import time
import random
from multiprocessing import Pool
from tqdm import tqdm

def myfunc(a):
    time.sleep(random.random())
    return a ** 2

if __name__ == __main__:
    pool = Pool(2)
    ‘‘‘
    for _ in tqdm(pool.imap_unordered(myfunc, range(100)), total=100):
        pass
    ‘‘‘
    pbar = tqdm(total=100)
    def update(*a):
        pbar.update()
        # tqdm.write(str(a))
    for i in range(pbar.total):
        pool.apply_async(myfunc, args=(i,), callback=update)
    # tqdm.write(‘scheduled‘)
    pool.close()
    pool.join()

 

原文:https://www.cnblogs.com/onetrainee/p/13347495.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!