it seems that I can't modify a global variable in Python when using a function which is called from pprocess. Here is my example:
import pprocess
import time
numbers=[0,0,0,0,0,0,0,0,0,0]
# find system time and store in global variable
def find_time(index):
global numbers
x=time.time()
print "Setting element %s of numbers to %f" % (index, x)
numbers[index]=x
return x
# parallel execution of the function
results=pprocess.pmap(find_time, [0,1,2,3,4,5,6,7,8,9], limit=6)
for y in results:
print '%f' % y
# this list is unchanged
print numbers
# serial execution of the function
for x in [0,1,2,3,4,5,6,7,8,9]:
find_time(x)
# now it seems to work
print numbers
"numbers" is just a list of zeros, and for the sake of demonstration I'm trying to set each list element to the current system time. When invoked using pprocess this doesn't work, but when I use a simple for loop to call the function then the global variable is changed.
I've spent some time reading about global variables and sincerely hope this isn't a trivial issue. Can anybody explain to me what is going on?
Many thanks,
Enno