SomeoneNew
SomeoneNew

Reputation: 91

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

I am using RPyC for a client-server application.

I try to change the value of exposed_variable when I call exposed_change() method. I receive "UnboundLocalError: local variable 'exposed_variable' referenced before assignment" error.

However, if I make exposed_variable global (before I try to modify it, like in this example), I get "NameError: name 'exposed_variable' is not defined".

What am I missing?

This is my server:

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()

This is my client:

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()

Thank you. I am wainting for your advice...

Upvotes: 1

Views: 686

Answers (1)

outis
outis

Reputation: 77400

(Originally from the question:)

Problem solved. Instead of "exposed_variable" I used a simple "variable" which I defined as global(under "imports"). I made it global in exposed_change() and i returned it. Now it's working as I expected.

Upvotes: 0

Related Questions