I have a global variable, called changed
. In a simple multiprocessing environment, I want to change this global variable dependent on the work of one dedicated worker. A minimal example to reproduce my output is:
import multiprocessing as mp
changed=False
def log_result(result):
global changed
if result==50:
print 'Callback changed'
#changed=True
def change(i):
global changed
if i==51:
print 'changed'
changed=True
print changed
return i
def parallel():
global changed
print 'Start Multiprocessing'
pool = mp.Pool(processes=4)
for i in range(100):
pool.apply_async(change, args = (i,), callback=log_result)
pool.close()
pool.join()
print "Multiprocessing done!"
parallel()
What I can see, that the worker itself seems not to able to change the global variable, instead, the value is only changed in the scope of the worker. As soon a I uncomment the
changed=True
line in the callback function log_result
, the variable is set as expected. Does that mean, that the callback function is not called in a parallel way by the worker processes as global variables are not sharable between processes.