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

Rutt, Benjamin Benjamin.Rutt at gs.com
Wed Apr 15 16:32:48 EDT 2009

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?

More information about the Twisted-Python mailing list