문제

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top