title: How to share a dictionary in python multiprocessing map_async function?
body: <p>I have one list <code>lst</code> in python. I want to call one function <code>f</code> on each item of this list. This function <code>f</code> calls a third-party function <code>g</code>. I also want to measure the time taken by each function call to <code>g</code> by each item in the list <code>lst</code>. I want to speed up the process so I use a multiprocessing pool to parallelize the execution. Currently, I have the following code, but it does not work. I have learned from <a href="https://stackoverflow.com/a/27479543">this</a> post that <code>map_async</code> can only call unary functions. I also want to take advantage of creating multiple processes in <code>map_async</code> and so I don't want to switch to <code>apply_async</code>. Can someone suggest me what is the better alternative here to achieve my goal?</p>
  <p>My current solution that does not work:</p>
  <pre class="lang-py prettyprint-override"><code>
  import multiprocessing as mp
  
  time_metrics = {}
  
  def f(idx):
      global time_metrics
      
      a = time.now()
      g(idx)
      b = time.now()
      time_metrics[idx] = b-a
  
  lst = [1, 2, 3, 4, 5, 6]
  pool = mp.Pool(7)
  pool.map_async(f, lst)
  pool.close()
  pool.join()
  
  print(time_metrics)
  
  </code></pre>