title: How can global variables be accessed when using Multiprocessing and Pool?
body: <p>I'm trying to avoid having to pass variables redundantly into <code>dataList</code> (e.g. <code>[(1, globalDict), (2, globalDict), (3, globalDict)]</code>) and use them globally instead. <code>global globalDict</code> is not a solution to do so in the following code, however. </p>
  
  <p>Is there a straightforward way to access data in a multiprocessing function globally?</p>
  
  <p>I read the following <a href="http://tding.in/2016/10/23/python-multiprocessing.html" rel="noreferrer">here</a>: </p>
  
  <p>"<strong>Communication is expensive.</strong>
  In contrast to communication between threads, exchanging data between processes is much more expensive. In Python, the data is pickled in to binary format before transferring on pipes. Hence, the overhead of communication can be very significant when the task is small. To reduce the extraneous cost, better assign tasks in chunk."</p>
  
  <p>I'm not sure if that would apply here, but I would like to simplify data access in any case.</p>
  
  <pre><code>def MPfunction(data):
      global globalDict
  
      data += 1
  
      # use globalDict
  
      return data
  
  if __name__ == '__main__':
  
      pool = mp.Pool(mp.cpu_count())
  
      try:
          globalDict = {'data':1}
  
          dataList = [0, 1, 2, 3]
          data = pool.map(MPfunction, dataList, chunksize=10)
  
      finally:
          pool.close()
          pool.join()
          pool.terminate()
  </code></pre>