Pyro4 和普通的旧数据对象

Pyro4 and plain old data objects

我有一个用 Pyro4 公开的对象,return是一个 POD 对象:

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()

但是当我尝试像这样检索这个对象时:

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

我遇到这样的异常:

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

有什么方法可以 return POD 对象并使用它们的字段,还是我需要通过方法或属性公开这些字段?

来自服务器端的文档,第 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.

因此您需要将 POD class 更改为:

@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