簡體   English   中英

我正在嘗試更改 RPyC 中暴露變量的值。 我收到一個錯誤。 我究竟做錯了什么?

[英]I am trying to change the value of an exposed variable in RPyC. I receive an error. What am I doing wrong?

我將 RPyC 用於客戶端-服務器應用程序。

當我調用explore_change() 方法時,我嘗試更改exposed_variable 的值。 我收到“UnboundLocalError:分配前引用的局部變量‘exposed_variable’”錯誤。

但是,如果我將暴露的變量設為全局(在我嘗試修改它之前,就像在這個例子中一樣),我會得到“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()

謝謝你。 我在等待你的建議...

(最初來自問題:)

問題解決了。 我使用了一個簡單的“變量”而不是“exposed_variable”,我將其定義為全局變量(在“導入”下)。 我在exposed_change() 中將其設為全局並返回了它。 現在它按我的預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM