调用 Pyro4 远程对象的方法永远卡住。

Calling a method of Pyro4 remote object stuck for ever.

下面是测试我遇到的这个问题的示例代码。 当调用服务器远程对象的方法 testmaster.test() 时,执行永远卡住了(实际上不确定这里是服务器还是客户端)。

即使是@Pyro4.callback也无济于事(不确定在这里是否合乎逻辑) 我正在使用 Python 2.7.12 和 Pyro4 我该如何解决这个问题,我们将不胜感激

#Run python -m Pyro4.naming in another terminal first:
import Pyro4

@Pyro4.expose
@Pyro4.callback
class Master:
    @Pyro4.expose
    @Pyro4.callback
    def test(self):
        print "this is test"

nameserver = Pyro4.locateNS('localhost', 9090)
deamon = Pyro4.Daemon()
uri = deamon.register(Master())
nameserver.register("Master", uri, safe=True)
testmaster=Pyro4.Proxy(uri)#Object of master to call some functions from it
print "before calling test" #this will be executed
testmaster.test()
print "after calling test" #but not this, it just stuck forever- how can I make it to be executed
deamon.requestLoop()

您必须 运行 守护程序在一个单独的进程中。这就是 Pyro 的用途:在其他进程中调用方法!

运行 它与您的其余代码处于同一过程中是没有意义的:那么您为什么要使用 Pyro?您没有将对象分布到不同的进程或机器上。

您的代码 'hangs' 的原因是因为 testmaster.test() 调用正在尝试连接到 Pyro 守护程序,但尚未 运行ning 到任何地方。所以它会挂起(直到套接字超时)。 daemon.requestLoop() 永远不会到达 - 但是,即使是,代码仍然是错误的:运行 守护进程并在同一程序中通过 Pyro 调用对象没有什么意义。

我建议至少阅读手册中的介绍以掌握基础知识:http://pythonhosted.org/Pyro4/intro.html#simple-example