0

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.

Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • The dupe talks about implementing a counter, but the exact same principle can be applied - use a `multiprocessing.Value` to represent your `changed` variable, and explicitly pass it to the child. – dano Apr 30 '15 at 16:57
  • The question on how to solve the problem is already indicated by my answer. I'm trying to understand if the callback function is different than the worker and if that means, that the call to the callback is not happening in parallel. – Dschoni Apr 30 '15 at 17:00
  • 1
    The callback is run in a background thread of the main process, not in the child process. See [this question](http://stackoverflow.com/questions/24770934/who-runs-the-callback-when-using-apply-async-method-of-a-multiprocessing-pool) – dano Apr 30 '15 at 17:12

1 Answers1

0

Globals aren't shared across processes. Setting a global in one process changes it in that process only.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Is there a way, to change that, without using a file? Like shared memory? And furthermore: Does that mean, that the callback function is not another process and thus not parallel processed? – Dschoni Apr 30 '15 at 16:52
  • There is multiprocessing.shared_memory now – Radio Controlled Oct 25 '20 at 07:51