1

我可以在我的 pyzmq 事件循环中捕获一个 KeyboardInterrupt:

try:
    ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
    pass

但这只会突然停止 ioloop。我想在清理后手动检测 KeyboardInterrupt 并关闭 ioloop。我怎样才能做到这一点?

4

1 回答 1

5

使用signal模块处理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()
于 2014-03-11T00:54:53.373 回答