Question

Using Pyro4 I haven't managed to successfully execute a callback from server to client.

The server script looks as follows:

class RobotController(object):
def __init__(self):
    self.robotStates = []

def moveDemo(self, motionTime):
    stopTime = datetime.datetime.now() + datetime.timedelta(0,motionTime)
    while datetime.datetime.now() < stopTime:
        print "Robot is moving..."
        time.sleep(1)
    print "Robot stopped"
    return 0

def doCallback(self, callback):
    print("server: doing callback 1 to client")
    try:
        callback.call1()
    except:
        print("got an exception from the callback.")
        print("".join(Pyro4.util.getPyroTraceback()))
    print("server: doing callback 2 to client")
    try:
        callback.call2()
    except:
        print("got an exception from the callback.")
        print("".join(Pyro4.util.getPyroTraceback()))
    print("server: callbacks done")



if __name__ == '__main__':
    robotController = RobotController()
    if os.name == 'posix':
        daemon = Pyro4.Daemon(host="192.168.1.104", port=8000); 
    else:
        daemon = Pyro4.Daemon(host="localhost", port=8000);
    Pyro4.Daemon.serveSimple(
    { robotController: "robotController"},
    ns=False,
    daemon=daemon,
    verbose = True
    )

and the client looks as follows:

class CallbackHandler(object):
    def crash(self):
        a=1
        b=0
        return a//b
    def call1(self):
        print("callback 1 received from server!")
        print("going to crash - you won't see the exception here, only on the server")
        return self.crash()
    @Pyro4.callback
    def call2(self):
        print("callback 2 received from server!")
        print("going to crash - but you will see the exception here too")
        return self.crash()


daemon = Pyro4.core.Daemon()
callback = CallbackHandler()
daemon.register(callback)

#robotController = Pyro4.Proxy("PYRO:robotController@192.168.1.104:8000")
robotController = Pyro4.Proxy("PYRO:robotController@localhost:8000")
robotController._pyroOneway.add("doCallback")
robotController.doCallback(callback)

When executing the command robotController.doCallback(callback), the method doCallback on server is executed, but the server cannot access the client. It returns the following:

Traceback (most recent call last):
  File "C:\LASTNO\Python Projects\PiBot\RobotServer\PyroServer\pyroServer.py", line 63, in doCallback
    callback.call2()
  File "C:\Python27\lib\site-packages\Pyro4\core.py", line 160, in __call__
    return self.__send(self.__name, args, kwargs)
  File "C:\Python27\lib\site-packages\Pyro4\core.py", line 286, in _pyroInvoke
    self.__pyroCreateConnection()
  File "C:\Python27\lib\site-packages\Pyro4\core.py", line 371, in __pyroCreateConnection
    raise ce
CommunicationError: cannot connect: [Errno 10061] No connection could be made because the target machine actively refused it

Does anyone know what could be the cause of the error and how to fix it? Thank you!

Était-ce utile?

La solution

I solved the problem by modifying the client code as follows:

class CallbackHandler(object):
    def crash(self):
        a=1
        b=0
        return a//b
    def call1(self):
        print("callback 1 received from server!")
        print("going to crash - you won't see the exception here, only on the server")
        return self.crash()
    @Pyro4.callback
    def call2(self):
        print("callback 2 received from server!")
        print("going to crash - but you will see the exception here too")
        return self.crash()


daemon = Pyro4.core.Daemon()
callback = CallbackHandler()
daemon.register(callback)

with Pyro4.core.Proxy("PYRO:robotController@localhost:8000") as server:

    server._pyroOneway.add("doCallback")
    server.doCallback(callback)
    motion = server.moveDemo(15)

print("waiting for callbacks to arrive...")
print("(ctrl-c/break the program once it's done)\n")
daemon.requestLoop()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top