Accessing an attribute of a multiprocessing Proxy of a class

后端 未结 4 653
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 03:14

I have a class that I want to share in a read-only fashion with children processes in a pool, so I prepared a proxy of a class but it didn\'t work. The following is a simpli

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 03:43

    Here's a less verbose alternative that I found to work well in practice. Not sure if there are any disadvantages.

    class TestClass:
        def __init__(self, a):
            self.a = a
        def b(self):
            print self.a
    
    def wrap_test_class(*args, **kwargs):
        obj = TestClass(*args, **kwargs)
        obj.get_a = lambda: obj.a
        return obj
    
    class MyManager(BaseManager): pass
    
    MyManager.register('test', wrap_test_class)
    

    This allows you to access a by calling proxy_object.get_a()

提交回复
热议问题