【问题标题】:Twisted ReconnectingClientFactory - auto reconnect or explicitly call connector.connect()?Twisted ReconnectingClientFactory - 自动重新连接或显式调用connector.connect()?
【发布时间】:2013-11-29 15:42:45
【问题描述】:

当使用 Twisted ReconnectingClientFactory 并且连接丢失时,我需要从 clientConnectionLost 方法中调用 connector.connect() 还是自动发生?

答案可能看起来很明显,因为它毕竟是 ReconnectingClientFactory,但 Twisted 文档说 here 让我想知道:

“调用connector.connect()可能有用——这将重新连接。”

“可能有用”一词的措辞和使用导致了这个问题,因为基本客户端工厂的 api 文档说了同样的话。

Max 的答案是正确的,但经过进一步研究,我认为“更正者”的答案如下:

def clientConnectionLost(self, connector, reason):
    # do stuff here that is unique to your own requirements, then:
    ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

这允许您执行应用程序所需的特殊操作,然后调用工厂代码以允许 Twisted 为您调用 retry()。

【问题讨论】:

    标签: twisted twisted.internet twisted.client


    【解决方案1】:

    我的旧答案并不完全正确。而是这样做:

    def clientConnectionLost(self, connector, reason):
        # do stuff here that is unique to your own requirements, then:
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
    

    这允许您执行应用程序所需的特殊操作 然后调用工厂代码让 Twisted 处理 为你调用 retry()。

    【讨论】:

    • 查看原始问题的补充
    【解决方案2】:

    调用 ReconnectingClientFactory.clientConnectionLost(self, connector, reason) 是正确的做法,因为它:

    1. 在调用 self.retry(即 键,因为连接可能由于调用 '停止尝试()'
    2. 将 self.connector 设置为传入的连接器。
    3. 调用 self.retry()(由于缺少传入的连接器,使用 #2 中设置的 self.connector)。
    4. 如果将来 ReconnectingClientFactory 实现发生更改,需要在重新连接路径中执行更多操作,则无需更改代码即可自动处理。

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2021-03-15
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      相关资源
      最近更新 更多