首页 文章

Twisted ReconnectingClientFactory - 自动重新连接或显式调用connector.connect()?

提问于
浏览
2

当使用Twisted ReconnectingClientFactory并且连接丢失时,我是否需要从clientConnectionLost方法中调用connector.connect()或自动执行此操作?

答案可能看起来很明显,因为它毕竟是ReconnectingClientFactory但Twisted文档说了一些让我想知道的东西:* 3037803_:

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

术语'可能有用'的措辞和用法导致了这个问题,因为基本客户工厂的api doc说同样的事情 .

Max的回答是正确的,但经过进一步的研究,我认为'校正'的答案如下:

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

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

2 回答

  • 3

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

    def clientConnectionLost(self,connector,reason):
    #在这里做的东西是你自己的要求所特有的,然后:
    ReconnectingClientFactory.clientConnectionLost(self,connector,reason)
    这允许您执行应用程序所需的专门操作,然后调用工厂代码以允许Twisted为您调用retry() .

  • 1

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

    • 在调用self.retry之前检查'self.continueTrying'(这是关键,因为连接可能因为调用'stopTrying()'而丢失

    • 将self.connector设置为传入的连接器 .

    • 调用self.retry()(由于缺少传入连接器,因此使用#2中设置的self.connector) .

    • 如果将来ReconnectingClientFactory实现发生了更改,需要在重新连接路径中执行更多操作,则无需更改代码即可自动处理它们 .

相关问题