title: Sharing state across multiple processes using the Python multiprocessing module
body: <p>I have a function that does a calculation and saves the state of the calculation in the <code>result</code> dictionary (default default argument). I first run it, then run several processes using the multiprocessing module. I need to run the function again in each of those parallel processes, but after this function has run once, I need the cached state to be returned, the value must not be recalculated. This requirement doesn't make sense in my example, but I can't think of a simple realistic argument that would require this restriction. Using a dict as mutable default argument works, but 
  this doesn't work with the multiprocessing module. What approach can I use to get the same effect?</p>
  
  <p>Note that the state value is something (a dictionary containing class values) that cannot be passed to the multiple processes as an argument afaik.</p>
  
  <p>The SO question <a href="https://stackoverflow.com/questions/6832554/python-multiprocessing-how-do-i-share-a-dict-among-multiple-processes">Python multiprocessing: How do I share a dict among multiple processes?</a> seems to cover similar ground. Perhaps I can use a Manager to do what I need, but it is not obvious how. Alternatively, one could perhaps save the value to a global object, per <a href="https://stackoverflow.com/a/4534956/350713">https://stackoverflow.com/a/4534956/350713</a>, but that doesn't seem very elegant.</p>
  
  <pre><code>def foo(result={}):
      if result:
          print "returning cached result"
          return result
      result[1] = 2
      return result
  
  def parafn():
      from multiprocessing import Pool
      pool = Pool(processes=2)
      arglist = []
      foo()
      for i in range(4):
          arglist.append({})
      results = []
      r = pool.map_async(foo, arglist, callback=results.append)
      r.get()
      r.wait()
      pool.close()
      pool.join()
      return results
  
  print parafn()
  </code></pre>
  
  <p>UPDATE: Thanks for the comments. I've got a working example now, posted below.</p>