首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyro:无法执行回调

Pyro:无法执行回调
EN

Stack Overflow用户
提问于 2014-03-02 21:24:40
回答 1查看 701关注 0票数 2

使用Pyro4,我还没有成功地执行从服务器到客户端的回调。

服务器脚本如下所示:

代码语言:javascript
复制
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
    )

客户看起来如下:

代码语言:javascript
复制
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)

在执行命令robotController.doCallback(回调)时,将执行服务器上的方法doCallback,但服务器无法访问客户端。它返回以下内容:

代码语言:javascript
复制
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

有谁知道什么可能是错误的原因,以及如何修复?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-03 08:24:20

我通过修改客户机代码解决了这个问题,如下所示:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22133687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档