I can catch a KeyboardInterrupt in my pyzmq eventloop:
try:
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
pass
But that just stops the ioloop abruptly. I'd like to detect KeyboardInterrupt and shutdown the ioloop manually after a cleanup. How can I do this?
Use the signal
module to handle SIGINT
:
import signal
from tornado.ioloop import IOLoop
def on_shutdown():
print('Shutting down')
IOLoop.instance().stop()
if __name__ == '__main__':
ioloop = IOLoop.instance()
signal.signal(signal.SIGINT, lambda sig, frame: ioloop.add_callback_from_signal(on_shutdown))
ioloop.start()