Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...