Loading

this works

  1. import multiprocessing as mp
  2. import time
  3.  
  4. def foo_pool(x):
  5.     time.sleep(2)
  6.     return x*x
  7.  
  8. result_list = []
  9. def log_result(result):
  10.     # This is called whenever foo_pool(i) returns a result.
  11.     # result_list is modified only by the main process, not the pool workers.
  12.     result_list.append(result)
  13.  
  14. def apply_async_with_callback():
  15.     pool = mp.Pool()
  16.     for i in range(10):
  17.         pool.apply_async(foo_pool, args = (i, ), callback = log_result)
  18.     pool.close()
  19.     pool.join()
  20.     print(result_list)
  21.  
  22. if __name__ == '__main__':
  23.     apply_async_with_callback()

Comments