UPDATE: For ease of reading, here is how to add a callback before the reactor gets shutdown:
reactor.addSystemEventTrigger('before', 'shutdown', callable)
Original question follows.
If I have a client connected to a server, and it’s chilling in the reactor main loop waiting for events, when I hit CTRL-C, I get a “Connection to the other side was lost in a non-clean fashion: Connection lost.” How can I set it up so that I know when a KeyboardInterrupt happens, so that I can do proper clean-up and disconnect cleanly? Or how can I implement a cleaner way to shutdown that doesn’t involve CTRL-C, if possible?
If you really, really want to catch C-c specifically, then you can do this in the usual way for a Python application – use
signal.signalto install a handler forSIGINTthat does whatever you want to do. If you invoke any Twisted APIs from the handler, make sure you usereactor.callFromThreadsince almost all other Twisted APIs are unsafe for invocation from signal handlers.However, if you’re really just interested in inserting some shutdown-time cleanup code, then you probably want to use
IService.stopService(or the mechanism in terms of which it is implemented,reactor.addSystemEventTrigger) instead.If you’re using
twistd, then usingIService.stopServiceis easy. You already have anApplicationobject with at least one service attached to it. You can add another one with a customstopServicemethod that does your shutdown work. The method is allowed to return aDeferred. If it does, then the shutdown process is paused until thatDeferredfires. This lets you clean up your connections nicely, even if that involves some more network (or any other asynchronous) operations.If you’re not using
twistd, then usingreactor.addSystemEventTriggerdirectly is probably easier. You can install a before shutdown trigger which will get called in the same circumstanceIService.stopServicewould have been called. This trigger (just any callable object) can also return aDeferredto delay shutdown. This is done with a call toreactor.addSystemEventTrigger('before', 'shutdown', callable)(sometime before shutdown is initiated, so that it’s already registered whenever shutdown does happen).service.tac gives an example of creating and using a custom service.
wxacceptance.py gives an example of using
addSystemEventTriggerand delaying shutdown by (an arbitrary) three seconds.Both of these mechanisms will give you notification whenever the reactor is stopping. This may be due to a C-c keystroke, or it may be because someone used
kill -INT ..., or it may be because somewherereactor.stop()was called. They all lead to reactor shutdown, and reactor shutdown always processes shutdown event triggers.