title: Python - apply_async doesn't execute function
body: <p>Hi I'm trying to use multiprocessing to speed up my code. However, the apply_async doesn't work for me. I tried to do a simple example like:</p>
  
  <pre><code>from multiprocessing.pool import Pool
  t = [0, 1, 2, 3, 4, 5]
  def cube(x):
      t[x] = x**3
  pool = Pool(processes=4)
  for i in range(6):
      pool.apply_async(cube, args=(i, ))
  for x in t:
      print(x)
  </code></pre>
  
  <p>It does not really change <code>t</code> as I would expect.</p>
  
  <p>My real code is like:</p>
  
  <pre><code>from multiprocessing.pool import Pool
  def func(a, b, c, d):
      #some calculations
      #save result to files
      #no return value
  lt = #list of possible value of a
  #set values to b, c, d
  p = Pool()
  for i in lt:
      p.apply_async(func, args=(i, b, c, d, ))
  </code></pre>
  
  <p>Where are the problems here?</p>
  
  <p>Thank you!</p>
  
  <hr>
  
  <p>Update: Thanks to the comments and answers, now I understand why my simple example won't work. However, I'm still in trouble with my real code. I have checked that my <code>func</code> does not rely on any global variable, so it seems not to be the same problem as my example code. </p>
  
  <p>As suggested, I added a return value to my <code>func</code>, now my code is:</p>
  
  <pre><code>f = Flux("reactor")
  d = Detector("Ge")
  mv = arange(-6, 1.5, 0.5)
  p = Pool()
  lt = ["uee", "dee"]
  for i in lt:
      re = p.apply_async(res, args=(i, d, f, mv, ))
      print(re.get())
  p.close()
  p.join()
  </code></pre>
  
  <p>Now I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/Shu/Documents/Programming/Python/Research/debug.py", line 35, in &lt;module&gt;
  print(re.get())
    File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 608, in get
  raise self._value
    File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 385, in _handle_tasks
  put(task)
    File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/connection.py", line 206, in send
  self._send_bytes(_ForkingPickler.dumps(obj))
    File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
  cls(buf, protocol).dump(obj)
  AttributeError: Can't pickle local object 'Flux.__init__.&lt;locals&gt;.&lt;lambda&gt;'
  </code></pre>