Python:Tornado ioloop在KeyboardInterrupt上毫无例外地被杀死

尼奥

当我在以下程序中^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 8.0 x64,
  • 在Win32上的Python 2.7.6(默认值,2013年11月10日,19:24:18)[MSC v.1500 32位(Intel)]
  • 龙卷风== 3.2
尼奥

我确定此问题是因为我在Windows上使用Git Bash控制台。当我使用常规命令提示符时,一切都会按预期进行。我怀疑Git Bash正在捕获^ C并杀死了该过程。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章