Multiprocessing : use tqdm to display a progress bar

后端 未结 8 487
情深已故
情深已故 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:44

    Use imap instead of map, which returns an iterator of processed values.

    from multiprocessing import Pool
    import tqdm
    import time
    
    def _foo(my_number):
       square = my_number * my_number
       time.sleep(1)
       return square 
    
    if __name__ == '__main__':
       with Pool(2) as p:
          r = list(tqdm.tqdm(p.imap(_foo, range(30)), total=30))
    

提交回复
热议问题