Multiprocessing : use tqdm to display a progress bar

后端 未结 8 498
情深已故
情深已故 2020-12-04 07:26

To make my code more \"pythonic\" and faster, I use \"multiprocessing\" and a map function to send it a) the function and b) the range of iterations.

The implanted s

8条回答
  •  一个人的身影
    2020-12-04 07:30

    This approach simple and it works.

    from multiprocessing.pool import ThreadPool
    import time
    from tqdm import tqdm
    
    def job():
        time.sleep(1)
        pbar.update()
    
    pool = ThreadPool(5)
    with tqdm(total=100) as pbar:
        for i in range(100):
            pool.apply_async(job)
        pool.close()
        pool.join()
    

提交回复
热议问题