[Twisted-Python] Writing a shutdown command that sends an ACK back

Jean-Paul Calderone exarkun at divmod.com
Wed Apr 15 17:35:00 EDT 2009

On Wed, 15 Apr 2009 16:32:48 -0400, "Rutt, Benjamin" <benjamin.rutt at gs.com> wrote:
>Is there a good way, in, say, a LineReceiver based server, to implement
>a bit of code in lineReceived() that, if you receive a line like, let's
>say, 'shutdown' from a client, you can call:
>1) self.transport.write('goodbye\n')  # send a goodbye message to the
>And then call
>2) self.transport.close() # close the client's socket
>And then
>3) reactor.stop()  # terminate the server
>The problem I'm finding is that since I believe self.transport.write()
>enqueues the writing for later, and self.transport.close() enqueues the
>closing for later, both to be picked up by the reactor soon after my
>lineReceived() returns, so then if you stop the reactor in #3, it won't
>be available anymore to carry out any enqueued tasks for you.  At least
>I think that's what's happening in my code.
>Is there any good workarounds?  E.g., is reactor.callLater(5,
>reactor.stop) too much of a hack?

If you want to delay reactor shutdown until the connection has closed,
you can put the reactor.stop() call into the protocol's connectionLost


More information about the Twisted-Python mailing list