[Twisted-Python] Restarting a reactor listener

Jean-Paul Calderone exarkun at divmod.com
Wed Aug 2 20:23:14 EDT 2006

On Wed, 2 Aug 2006 16:12:49 -0700, Travis Vachon <tvachon at gmail.com> wrote:
>Hi Ian
>I ran into this just recently, but am not entirely certain I found the best
>solution. I needed to be able to switch the port the reactor was listening
>on without restarting the reactor. Here's how I did it:
>     def startListener(self, port, interface=""):
>        self.iListeningPort = reactor.listenTCP(
>            port,
>            self.factory,
>            interface=interface
>            )
>    def restartListener(self, port, interface=""):
>        self.iListeningPort.stopListening()
>        self.startListener(port, interface)
>I'm not sure if doing this will have any unintended side-effects, but so far
>it seems to have done the trick. If someone else on the list could confirm
>this does what I think it does (and what Ian is looking for) that would be

This is pretty much exactly correct.  The only thing which *might* be an
issue is that stopListening is not guaranteed to succeed immediately.  *If*
you are relying on the old port number to be available immediately after
restartListener returns, you might be surprised.  You can address this by
returning the Deferred which is returned by stopListening from restartListener
so that the caller will receive notification when the old port number becomes

In practice, and for the typical use cases, this probably won't often be a


More information about the Twisted-Python mailing list