3

I have a fairly complex object I need to share access to across processes in Python. It has a few @property methods/attributes on it which the AutoProxy doesn't seem to process. How can I expose these through the manager?

from multiprocessing.managers import BaseManager

class Foo(object):

    def __init__(self, a):
        self._a = a

    @property
    def a(self):
        return self._a

class FooManager(BaseManager): pass

FooManager.register("Foo", Foo)

if __name__ == "__main__":
    fmgr = FooManager()
    fmgr.start()
    foo = fmgr.Foo(5)
    # Below causes error
    foo.a

And the error I get is:

Traceback (most recent call last): File "test.py", line 26, in foo.a AttributeError: 'AutoProxy[Foo]' object has no attribute 'a'

EDIT

So as mentioned in the comments below, one approach is to also define this:

class FooProxy(NamespaceProxy):
    _exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'a')

And register the Foo object as:

FooManager.register("Foo", Foo, FooProxy)

While this seems to work fine if anyone knows a more automatic process to incorporate any and all @property objects that would be terrific.

Doug
  • 108
  • 8
  • 1
    ive never messed with the managers (tbh i didnt even know there was such a thing ... thats kind of cool .. ) now im somewhat curious and might dig a little deeper, however this answer looks like it might shed *some* light https://stackoverflow.com/questions/26499548/accessing-an-attribute-of-a-multiprocessing-proxy-of-a-class – Joran Beasley Oct 12 '17 at 17:11
  • Yeah I saw that one, I was hoping there was some other way to do it in a more automated fashion instead of explicitly exposing each attribute (there's a good amount of them). If there isn't some more automatic way then I suppose that's the way to do it for each individual attribute/property. – Doug Oct 12 '17 at 17:15
  • 1
    or just make them not properties and make them methods and interact with them as methods – Joran Beasley Oct 12 '17 at 17:17
  • Yes - that would work - issue is the actual implementation is used all over the place by many other classes/functions which assume access to the properties. – Doug Oct 12 '17 at 17:18
  • So as @JoranBeasley pointed out from the post mentioned in the first comment, I can create a: FooProxy object and add all the property calls to the _exposed_ attribute. If anyone knows a more automatic way that's great, otherwise this approach seems to work. – Doug Oct 12 '17 at 17:43

0 Answers0