<i id='62rX7'><tr id='62rX7'><dt id='62rX7'><q id='62rX7'><span id='62rX7'><b id='62rX7'><form id='62rX7'><ins id='62rX7'></ins><ul id='62rX7'></ul><sub id='62rX7'></sub></form><legend id='62rX7'></legend><bdo id='62rX7'><pre id='62rX7'><center id='62rX7'></center></pre></bdo></b><th id='62rX7'></th></span></q></dt></tr></i><div id='62rX7'><tfoot id='62rX7'></tfoot><dl id='62rX7'><fieldset id='62rX7'></fieldset></dl></div>
  1. <small id='62rX7'></small><noframes id='62rX7'>

    <legend id='62rX7'><style id='62rX7'><dir id='62rX7'><q id='62rX7'></q></dir></style></legend>
    <tfoot id='62rX7'></tfoot>

        <bdo id='62rX7'></bdo><ul id='62rX7'></ul>

      多处理进程中的共享状态

      时间:2023-05-26
        <bdo id='Z6mTN'></bdo><ul id='Z6mTN'></ul>

        1. <legend id='Z6mTN'><style id='Z6mTN'><dir id='Z6mTN'><q id='Z6mTN'></q></dir></style></legend>
          • <i id='Z6mTN'><tr id='Z6mTN'><dt id='Z6mTN'><q id='Z6mTN'><span id='Z6mTN'><b id='Z6mTN'><form id='Z6mTN'><ins id='Z6mTN'></ins><ul id='Z6mTN'></ul><sub id='Z6mTN'></sub></form><legend id='Z6mTN'></legend><bdo id='Z6mTN'><pre id='Z6mTN'><center id='Z6mTN'></center></pre></bdo></b><th id='Z6mTN'></th></span></q></dt></tr></i><div id='Z6mTN'><tfoot id='Z6mTN'></tfoot><dl id='Z6mTN'><fieldset id='Z6mTN'></fieldset></dl></div>

            <small id='Z6mTN'></small><noframes id='Z6mTN'>

              <tbody id='Z6mTN'></tbody>

            1. <tfoot id='Z6mTN'></tfoot>
                本文介绍了多处理进程中的共享状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                请考虑以下代码:

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

                这给出了输出

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

                但是,我希望它能给

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

                ..因为在调用触发方法时,h.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.

                事实上,如果您仔细阅读该模块,您可以通过 显式通信,或通过 显式共享对象(属于非常有限的语言子集,必须由 Manager).

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

                这篇关于多处理进程中的共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

              • <small id='hoyxD'></small><noframes id='hoyxD'>

                <legend id='hoyxD'><style id='hoyxD'><dir id='hoyxD'><q id='hoyxD'></q></dir></style></legend>
                  <bdo id='hoyxD'></bdo><ul id='hoyxD'></ul>
                  <i id='hoyxD'><tr id='hoyxD'><dt id='hoyxD'><q id='hoyxD'><span id='hoyxD'><b id='hoyxD'><form id='hoyxD'><ins id='hoyxD'></ins><ul id='hoyxD'></ul><sub id='hoyxD'></sub></form><legend id='hoyxD'></legend><bdo id='hoyxD'><pre id='hoyxD'><center id='hoyxD'></center></pre></bdo></b><th id='hoyxD'></th></span></q></dt></tr></i><div id='hoyxD'><tfoot id='hoyxD'></tfoot><dl id='hoyxD'><fieldset id='hoyxD'></fieldset></dl></div>

                          <tbody id='hoyxD'></tbody>

                      • <tfoot id='hoyxD'></tfoot>