当我在以下程序中^C
阻止龙卷风时按ioloop.start()
,Python将立即退出,并且不会引发KeyboardInterrupt(或任何其他异常)。发生了什么事,我该如何捕捉^C
?
import tornado.ioloop
import tornado.web
ioloop = tornado.ioloop.IOLoop.instance()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
# has no effect
# tornado.ioloop.PeriodicCallback(lambda:None, 1000).start()
print 'starting'
try:
ioloop.start()
except KeyboardInterrupt:
print '^C pressed'
finally:
print 'done'
输出:
$ /c/Python27x32/python test.py
starting
$
预期产量:
$ /c/Python27x32/python test.py
starting
^C pressed
done
$
我在跑:
我确定此问题是因为我在Windows上使用Git Bash控制台。当我使用常规命令提示符时,一切都会按预期进行。我怀疑Git Bash正在捕获^ C并杀死了该过程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句