下面是测试这个问题的示例代码。 打电话的时候,死刑就一直被执行testmaster.test()它是服务器远程对象的一种方法(实际上不确定它是服务器还是客户端)。在
即使@Pyro4.callback也没有帮助(不确定是否符合逻辑) 我使用的是python2.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呢?你不能在不同的进程或机器上分配你的对象。在
代码“挂起”的原因是,
testmaster.test()
调用试图连接到一个Pyro守护进程,但该守护进程尚未在任何地方运行。所以它会挂起(直到插座超时)。从未到达daemon.requestLoop()
,但即使是这样,代码还是错误的:在同一个程序中运行守护进程并通过Pyro调用对象没有什么意义。在我建议阅读手册中的介绍,至少要掌握基本知识:http://pythonhosted.org/Pyro4/intro.html#simple-example
相关问题 更多 >
编程相关推荐