【发布时间】:2018-06-08 08:15:32
【问题描述】:
我正在将 RPyC 用于客户端-服务器应用程序。
当我调用exposed_change() 方法时,我尝试更改exposed_variable 的值。 我收到“UnboundLocalError: local variable 'exposed_variable' referenced before assignment”错误。
但是,如果我将exposed_variable 设为全局(在尝试修改它之前,例如在this example 中),我会收到“NameError: name 'exposed_variable' is not defined”。
我错过了什么?
这是我的服务器:
from rpyc.utils import server
import rpyc
import time
class DoStuffService(rpyc.Service):
exposed_variable = 1
def exposed_change(self):
#global exposed_variable
exposed_variable = exposed_variable + 1
if __name__ == '__main__':
protocol_config = dict(instantiate_custom_exceptions=True, import_custom_exceptions=True)
server.ThreadedServer(DoStuffService, hostname='localhost', port=8888, auto_register=False,protocol_config=protocol_config, backlog=500).start()
这是我的客户:
import rpyc, sys
import time
def rpyc_call():
conn = rpyc.connect('localhost', 8888)
a = 1
while a:
conn.root.change()
nr=conn.root.variable
print("Nr is ", nr)
time.sleep(10)
if __name__ == '__main__':
rpyc_call()
谢谢。我正在等待您的建议...
【问题讨论】:
标签: python python-3.x client-server rpyc