Multiprocessing : use tqdm to display a progress bar

后端 未结 8 484
情深已故
情深已故 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条回答
  •  萌比男神i
    2020-12-04 07:42

    Sorry for being late but if all you need is a concurrent map, I added this functionality in tqdm>=4.42.0:

    from tqdm.contrib.concurrent import process_map  # or thread_map
    import time
    
    def _foo(my_number):
       square = my_number * my_number
       time.sleep(1)
       return square 
    
    if __name__ == '__main__':
       r = process_map(_foo, range(0, 30), max_workers=2)
    

    References: https://tqdm.github.io/docs/contrib.concurrent/ and https://github.com/tqdm/tqdm/blob/master/examples/parallel_bars.py

    It supports max_workers and chunksize and you can also easily switch from process_map to thread_map.

提交回复
热议问题