【问题标题】:I am trying to change the value of an exposed variable in RPyC. I receive an error. What am I doing wrong?我正在尝试更改 RPyC 中公开变量的值。我收到一个错误。我究竟做错了什么?
【发布时间】: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


【解决方案1】:

(最初来自问题:)

问题解决了。我没有使用“exposed_variable”,而是使用了一个简单的“变量”,我将其定义为全局(在“imports”下)。我在exposed_change() 中将其设为全局并返回了它。现在它正在按我的预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多