pythonpython-3.xpyro4

Pyro4 and plain old data objects


I have an object which I expose with Pyro4 and which returns a POD object:

import Pyro4

@Pyro4.expose
class POD:
    def __init__(self):
        self.a = 1

@Pyro4.expose
class Source:
    def get(self):
        p = POD()
        daemon.register(p)
        return p

daemon = Pyro4.daemon()
ns = Pyro4.locateNS()
uri = daemon.register(Source)
ns.register('test_obj', uri)
daemon.requestLoop()

But when I try to retrieve this object like this:

import Pyro4
s = Pyro4.Proxy('PYRONAME:test_obj')
o = s.get()
print(o.a)

I get an exception like this:

Pyro4.errors.PyroError: remote object doesn't expose any methods or attributes. Did you forget setting @expose on them?

Is there some way to return POD objects and use their fields, or do I need to expose these fields through methods or properties?


Solution

  • From the documentation for the server side, section Creating a Pyro class and exposing its methods and properties:

    You can’t expose attributes directly. It is required to provide a @property for them and decorate that with @expose, if you want to provide a remotely accessible attribute.

    So you need to change the POD class to:

    @Pyro4.expose
    class POD:
        def __init__(self):
            self._a = 1
    
        @property
        def a(self):
            return self._a
    
        # Only necessary when setting the property should be possible.
        @a.setter
        def a(self, value):
            self._a = value