Shared state in Python multiprocessing processes

Please consider this code:

import time
from multiprocessing import Process

class Host(object):
    def __init__(self):
        self.id = None
    def callback(self):
        print "self.id = %s" % self.id
    def bind(self, event_source):
        event_source.callback = self.callback

class Event(object):
    def __init__(self):
        self.callback = None
    def trigger(self):
        self.callback()

h = Host()
h.id = "A"
e = Event()
h.bind(e)
e.trigger()

def delayed_trigger(f, delay):
    time.sleep(delay)
    f()

p = Process(target = delayed_trigger, args = (e.trigger, 3,))
p.start()

h.id = "B"
e.trigger()

      

This gives in the output

self.id = A
self.id = B
self.id = A

      

However, I expected it to give

self.id = A
self.id = B
self.id = B

      

.. because h.id has already been changed to "B" by the time the trigger method was called.

It seems that a node instance is instantiated when a separate Process is started, so changes to the original host do not affect that copy.

In my project (in more detail, of course), host instance fields change from time to time, and it is important that events that are triggered by code running in a separate process have access to those changes.

+2


source to share


1 answer


multiprocessing runs stuff in separate processes . It is incomprehensible that things are not copied as they are sent, since exchanging files between processes requires shared memory or communication.



In fact, if you look at the module, you can see the amount of effort required to actually exchange information between processes after divergence, either through explicit communication or through explicitly separated objects (which have a very limited subset of the language and must be managed Manager

).

+4


source







All Articles