微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在进程之间共享类的最佳方式

如何解决在进程之间共享类的最佳方式

首先,我是多处理方面的新手,我是来向大家学习的。我有几个文件做类似的事情:

SharedClass.py:

class simpleClass():
    a   = 0
    b   = ""
    .....

MyProcess.py:

import multiprocessing
import SharedClass
class FirstProcess(multiprocessing.Process):
    def __init__(self):
    multiprocessing.Process.__init__(self)

    def modifySharedClass():
        # Here I want to modify the object shared with main.py defined in SharedClass.py

Main.py:

from MyProcess  import FirstProcess
import sharedClass
if __name__ == '__main__':
    pr = FirstProcess()
    pr.start()
    # Here I want to print the initial value of the shared class
    pr.modifySharedClass()
    # Here I want to print the modified value of the shared class

我想定义一个共享类(在 SharedClass.py 中),在一种共享内存中,可以为 Main.py 和 MyProcess.py 文件读写。

我尝试使用多处理的 Managermultiprocessing.array 但我没有得到好的结果,在一个文件中所做的更改没有反映在另一个文件中(也许我在方法不对)。

有什么想法吗?谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。