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

问题描述

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

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 (将#修改为@)