开发者

Restarting a Twisted-python Reactor after an unsuccessful connection

开发者 https://www.devze.com 2023-02-13 00:24 出处:网络
I am writing a server with multiple clients. When a client starts, the server may not yet be working. So a reactor.connectTCP may fail (no receiving end). Currently I\'m solving this by looping on a r

I am writing a server with multiple clients. When a client starts, the server may not yet be working. So a reactor.connectTCP may fail (no receiving end). Currently I'm solving this by looping on a reactor.run, i.e.:

  1. connect to server
  2. reactor.run
  3. if fails, repeat

I understand this is not开发者_如何学Python the way to do it in twisted. How can I do it then?


You can always try to reconnect within your connectionLost handler, for example:

from twisted.internet.protocol import ClientFactory

class EchoClientFactory(ClientFactory):
    def clientConnectionLost(self, connector, reason):
        connector.connect()

There is also even a built-in ReconnectingClientFactory. See also: this blurb on reconnection.

0

精彩评论

暂无评论...
验证码 换一张
取 消