2

Pyro4.Daemon 对象的 requestLoop 方法存在一些问题。

我想要的是远程调用“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()

客户

import Pyro4

def main():

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

我期望看到服务器打印“hello”和“another hi”,然后关闭。

但是关闭并没有发生,服务器仍然在 requestloop 方法中被阻塞。只要我愿意,我就可以使用我的代理。

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

Pyro4.errors.ConnectionClosedError: receiving: not enough data

我所有的测试都说我需要创建第二个代理并抛出异常以在我的服务器上传递请求循环。

有没有人知道如何清理这个问题?

4

1 回答 1

4

如果您查看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检查它何时被调用。如果没有上面的配置,您会看到该方法似乎仅在接收到新事件时才执行,因此在接收到设置running为的下一个事件后服务器不会关闭False。例如,如果您执行两次客户端程序,服务器将关闭。

于 2011-12-14T17:10:11.700 回答