2011-12-14 108 views
2

我对Pyro4.Daemon对象的requestLoop方法有一些问题。即使在loopCondition为False之后,requestloop(loopCondition)也不会释放

我想要的是远程调用“stop()”方法来释放requestLoop函数并关闭守护进程。

这个小为例不起作用

服务器

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
from daemon import Pyro4 

class Audit(object): 
    def start_audit(self): 
     with Pyro4.Daemon() as daemon: 
      self_uri = daemon.register(self) 
      ns = Pyro4.locateNS() 
      ns.register("Audit", self_uri) 
      self.running = True 
      print("starting") 
      daemon.requestLoop(loopCondition=self.still_running) 
      print("stopped") 
      self.running = None 

    def hi(self, string): 
     print string 

    def stop(self): 
     self.running = False 

    def still_running(self): 
     return self.running 

def main(): 

    # lancement de l'auditor 
    auditor = Audit() 
    auditor.start_audit() 

if __name__ == "__main__" : 
    main() 

CLIENT

import Pyro4 

def main(): 

    with Pyro4.Proxy("PYRONAME:Audit") as au: 
     au.hi("hello") 
     au.hi("another hi") 
     au.stop() 

我想到的是要看到服务器打印 “你好” 和 “另一个喜”,然后关掉。

但是关闭不会发生,服务器仍然在请求回滚方法中被阻塞。 只要我想要,我可以使用我的代理。

,但如果我创建另一个客户端,在第一次远程调用,服务器将关闭,客户端将引发错误:

Pyro4.errors.ConnectionClosedError: receiving: not enough data 

我所有的测试都在说,我需要创建一个第二代理并抛出这个exeption来传递requestloop到我的服务器上。

有没有人有如何清理这个问题的想法?

回答

2

如果你看一下在源examples/callback/client.py你会看到这样的评论:

# We need to set either a socket communication timeout, 
# or use the select based server. Otherwise the daemon requestLoop 
# will block indefinitely and is never able to evaluate the loopCondition. 
Pyro4.config.COMMTIMEOUT=0.5 

因此,你需要做的是设置在您的服务器文件COMMTIMEOUT,它会根据正常工作我试验。

注意:您还可以将print语句添加到still_running方法以检查它何时被调用。如果没有上面的配置,你会发现它看起来只有在收到新事件时才会执行该方法,所以在接收到下一个事件后,服务器不会关闭,收到的设置为runningFalse。例如,如果您执行两次客户端程序,服务器将关闭。

+0

它只是工作正常。非常感谢你。 我读过一些关于TIMEOUT的文档,说requestloop有3秒的超时时间。也许是旧版本? – ornoone 2011-12-15 09:07:39

相关问题