zoukankan      html  css  js  c++  java
  • Python进程池中实现进度条显示

    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()
  • 相关阅读:
    数据分析(三)
    数据分析(二)
    数据分析(一)
    sql server 脚本创建数据库和表
    各种距离分析
    DataTable数据导出CSV文件
    WPF中Grid布局
    111
    123
    SVN的安装与使用
  • 原文地址:https://www.cnblogs.com/onetrainee/p/13347495.html
Copyright © 2011-2022 走看看