【问题标题】:How do I detect a lost client connection in a server that inherits from pb.Root?如何检测从 pb.Root 继承的服务器中丢失的客户端连接?
【发布时间】:2010-02-02 14:25:19
【问题描述】:

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

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,(通过遍历可能连接的客户端列表)但这是一种非事件驱动的方式 - 坦率地说为时已晚。

欢迎任何想法。

提前致谢。

【问题讨论】:

    标签: python twisted


    【解决方案1】:

    您可以注册一个回调,以便在连接丢失时调用。为此有两个API,一个是Broker.notifyOnDisconnect,另一个是RemoteReference.notifyOnDisconnect。它们做同样的事情,但根据您的应用程序的详细信息,其中一种可能更方便访问。

    我不确定你是否可以用它来保证你永远不会得到DeadReferenceError(例如,我不确定如果你的remote_foo方法会发生什么被调用,你返回一个 Deferred,连接丢失,然后 Deferred 触发),但你至少可以大大减少常见情况下的可能性(即,你应该能够始终避免从 @987654326 获取 DeadReferenceError @ 如果你在notifyOnDisconnect 回调你的函数后从不使用callRemote)。

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多