41

Possible Duplicate:
Python multiprocessing global variable updates not returned to parent

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:

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

As well as

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

I expect the result to be [1, 2, 3, 4, 5] but the result is [0, 1, 2, 3, 4].

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?

Community
  • 1
  • 1
user1475412
  • 1,659
  • 2
  • 22
  • 30
  • Look at this question http://stackoverflow.com/questions/659865/python-multiprocessing-sharing-a-large-read-only-object-between-processes – 8bitwide Jun 26 '12 at 20:43
  • Are you running these code snippets from a script, or executing inside a Python console ? – Christian Witts Jun 26 '12 at 20:48
  • 1
    You should manage your list with a multiprocessing Manager. – mgilson Jun 26 '12 at 20:48
  • Any changes to the global variable made by the multiprocessing thread will not be visible in the parent as the thread receives a copy-on-write view of the parent memory – Gearoid Murphy Jun 26 '20 at 20:45

1 Answers1

30

If you are running two separate processes, then they won't be sharing the same globals. If you want to pass the data between the processes, look at using send and recv. Take a look at http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes for an example similar to what you're doing.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38