我正在应用程序中使用rpyc,遇到了一个小问题。
我只想知道是否有可能更新远程属性?
我有一个测试服务器:
import rpyc
from rpyc.utils.server import ThreadedServer
class Test:
def __init__(self):
self._v = 0
@property
def V(self):
return self._v
@V.setter
def V(self, value):
self._v = value
class TestService(rpyc.Service):
def exposed_Test(self):
return Test()
if __name__ == '__main__':
t = ThreadedServer(TestService, port = 2942,
protocol_config={"allow_all_attrs":True})
t.start()在ipython控制台中:
In [1]: import rpyc
In [2]: conn = rpyc.connect('localhost', 2942, config={'allow_all_attrs':True})
In [3]: test = conn.root.Test()
In [4]: test.V
Out[4]: 0
In [5]: test.V = 2
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-d3ae0dcd1075> in <module>()
----> 1 test.V = 2
<blah blah traceback>
AttributeError: cannot access 'V'是否有可能更新远程属性?
发布于 2016-01-18 20:35:49
是的,默认情况下设置远程属性是被阻塞的,但如果设置allow_setattr=True,则允许设置远程属性。
(如果您正在设置allow_delattr=True,那么设置allow_setattr=True也是有意义的)
t = ThreadedServer(TestService, port = 2942,
protocol_config={"allow_all_attrs":True,
"allow_setattr": True,
"allow_delattr": True,})见医生们。
您还可以绕过setattr保护,直接访问远程对象的__dict__ (当然,第一个解决方案要好得多):
test.__dict__['V'] = 2https://stackoverflow.com/questions/34844127
复制相似问题