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.signal
to install a handler for SIGINT
that does whatever you want to do. If you invoke any Twisted APIs from the handler, make sure you use reactor.callFromThread
since 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 using IService.stopService
is easy. You already have an Application
object with at least one service attached to it. You can add another one with a custom stopService
method that does your shutdown work. The method is allowed to return a Deferred
. If it does, then the shutdown process is paused until that Deferred
fires. 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 using reactor.addSystemEventTrigger
directly is probably easier. You can install a before shutdown trigger which will get called in the same circumstance IService.stopService
would have been called. This trigger (just any callable object) can also return a Deferred
to delay shutdown. This is done with a call to reactor.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 addSystemEventTrigger
and 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 somewhere reactor.stop()
was called. They all lead to reactor shutdown, and reactor shutdown always processes shutdown event triggers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With