Python类对象在工作进程之间共享

时间:2016-03-01 10:24:09

标签: python python-2.7

这是我的代码。我不明白为什么它给出错误:AttributeError:'AutoProxy [MySharedClass]'对象没有属性'dict_'     进口时间     导入多处理     来自多处理导入过程,经理     将multiprocessing.managers导入为管理员

class MySharedClass(object):
    def __init__(self):
        self.dict_ = {}
        self.dict_['one'] = 1
        self.dict_['two'] = 2

class MyManager(manager.BaseManager):
    pass

def test_process(param_object):
    print"Child Process:"
    print param_object.dict_
    param_object.dict_['three'] = 3
    print param_object.dict_
    return param_object


def main_process():
    MyManager.register("MySharedClass", MySharedClass)
    my_manager = MyManager()
    my_manager.start()

    param_object = my_manager.MySharedClass()
    print"Main process:"
    print param_object.dict_
    time.sleep(.2)
    proc = Process(target=test_process, args=(param_object,))
    proc.start()
    print"Process created with id %s" %proc.pid

    time.sleep(10)

    print "Main process End:"
    print param_object.dict_

main_process()

0 个答案:

没有答案