gpt4 book ai didi

python - Pyro4 和普通的旧数据对象

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:36 25 4
gpt4 key购买 nike

我有一个对象,我用 Pyro4 公开它并返回一个 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?

有什么方法可以返回 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 类更改为:

@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

关于python - Pyro4 和普通的旧数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50352337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com