如何在继承的服务器中检测丢失的客户端连接铅根?

2024-04-20 05:38:11 发布

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

例如,我有一个客户端,它通过以下方式连接到服务器:

class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory):
    def __init__(self):
        pb.PBClientFactory.__init__(self)
        self.ipaddress = None

    def clientConnectionMade(self, broker):
        log.msg('Started to connect.')
        pb.PBClientFactory.clientConnectionMade(self, broker)

    def buildProtocol(self, addr):
        log.msg('Connected to %s' % addr)
        return pb.PBClientFactory.buildProtocol(self, addr)

    def clientConnectionLost(self, connector, reason):
        log.msg('Lost connection.  Reason:', reason)
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        log.msg('Connection failed. Reason:', reason)
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

因此客户端可以自动检测连接何时丢失。在

如果客户机宕机,例如崩溃,如何从服务器获得相同的行为?在

我目前捕获了一个DeadReferenceError(通过迭代潜在连接的客户机列表),但这是一种非事件驱动的方式-坦白地说,太晚了。在

欢迎有任何想法。在

提前谢谢。在


Tags: self服务器log客户端connectorinitdef方式
1条回答
网友
1楼 · 发布于 2024-04-20 05:38:11

您可以注册一个回调,以便在连接丢失时调用。有两个api用于此,一个是Broker.notifyOnDisconnect,另一个是RemoteReference.notifyOnDisconnect。它们执行相同的操作,但根据应用程序的详细信息,其中一个或另一个可能更方便访问。在

我不确定你是否可以用这个来保证你永远不会得到DeadReferenceError(例如,我不确定如果一个remote_foo方法被调用,你返回一个延迟的,连接丢失,然后延迟触发),但是你至少可以大大降低常见情况下的可能性(即,如果在notifyOnDisconnect调用函数后从未使用callRemote,则应该始终避免从callRemote获取{}。在

相关问题 更多 >