请求循环(循环条件)即使在循环条件为假后也不会释放

2024-05-21 10:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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()

客户

^{pr2}$

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

但是关闭没有发生,服务器仍然被阻塞在requestloop方法中。 我可以随时使用我的代理人。在

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

Pyro4.errors.ConnectionClosedError: receiving: not enough data

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

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


Tags: 方法self服务器远程maindefauditauditor
1条回答
网友
1楼 · 发布于 2024-05-21 10:30:38

如果您查看源代码中的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的下一个事件之后,服务器不会关闭。例如,如果您执行客户机程序两次,服务器将关闭。在

相关问题 更多 >