[Twisted-Python] Writing a shutdown command that sends an ACK back
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 client 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?
On Wed, 15 Apr 2009 16:32:48 -0400, "Rutt, Benjamin" <benjamin.rutt@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 client
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 callback. Jean-Paul
participants (2)
-
Jean-Paul Calderone
-
Rutt, Benjamin