Python 每 2 秒重新连接一次的 Twisted 客户端实现
Python Twisted client implementation with reconnection every 2 seconds
我使用 ReconnectingClientFactory
来实现具有重新连接功能的客户端。
# ...
class ClientProtocol(Protocol):
def __init__(self, factory):
self.factory = factory
# ...
class ClientFactory(ReconnectingClientFactory):
def buildProtocol(self, addr):
self.resetDelay()
return ClientProtocol(self)
def clientConnectionFailed(self, connector, reason):
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason):
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
# ...
它有效,但重新连接延迟在失败后呈指数增长。延迟两秒的最佳方法是什么?
将maxDelay
设为2:
class YourClientFactory(ReconnectingClientFactory):
maxDelay = 2.0
# ...
我使用 ReconnectingClientFactory
来实现具有重新连接功能的客户端。
# ...
class ClientProtocol(Protocol):
def __init__(self, factory):
self.factory = factory
# ...
class ClientFactory(ReconnectingClientFactory):
def buildProtocol(self, addr):
self.resetDelay()
return ClientProtocol(self)
def clientConnectionFailed(self, connector, reason):
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason):
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
# ...
它有效,但重新连接延迟在失败后呈指数增长。延迟两秒的最佳方法是什么?
将maxDelay
设为2:
class YourClientFactory(ReconnectingClientFactory):
maxDelay = 2.0
# ...