title: Globals variables and Python multiprocessing
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/11055303/python-multiprocessing-global-variable-updates-not-returned-to-parent">Python multiprocessing global variable updates not returned to parent</a>  </p>
  </blockquote>
  
  
  
  <p>I am using a computer with many cores and for performance benefits I should really use more than one. However, I'm confused why these bits of code don't do what I expect:</p>
  
  <pre><code>from multiprocessing import Process
  
  var = range(5)
  def test_func(i):
      global var
      var[i] += 1
  
  if __name__ == '__main__':
      jobs = []
      for i in xrange(5):
          p = Process(target=test_func,args=(i,))
          jobs.append(p)
          p.start()
  
  print var
  </code></pre>
  
  <p>As well as</p>
  
  <pre><code>from multiprocessing import Pool
  
  var = range(5)
  def test_func(i):
      global var
      var[i] += 1
  
  if __name__ == '__main__':
      p = Pool()
      for i in xrange(5):
          p.apply_async(test_func,[i])
  
  print var
  </code></pre>
  
  <p>I expect the result to be <code>[1, 2, 3, 4, 5]</code> but the result is <code>[0, 1, 2, 3, 4]</code>.</p>
  
  <p>There must be some subtlety I'm missing in using global variables with processes. Is this even the way to go or should I avoid trying to change a variable in this manner?</p>