简体   繁体   中英

Shared state in 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 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 the h.id was already changed to "B" by the time the trigger method was called.

It seems that a copy of host instance is created at the moment when the separate Process is started, so the changes in the original host do not influence that copy.

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

multiprocessing runs stuff in separate processes . It is almost inconceivable that things are not copied as they're sent, as sharing stuff between processes requires shared memory or communication.

In fact, if you peruse the module, you can see the amount of effort it takes to actually share anything between the processes after the diverge, either through explicit communication , or through explicitly-shared objects (which are of a very limited subset of the language, and have to be managed by a Manager ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM