2014-02-16 21 views
1

当我按下^C在下面的程序中阻塞龙卷风的ioloop.start()时,Python立即退出并且没有发生KeyboardInterrupt(或任何其他异常)。发生了什么,我该如何捕获^CPython:Tornado ioloop在KeyboardInterrupt上没有任何例外被杀死

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,
  • 的Python 2.7.6(默认,11月10日2 013,19时24分18秒)[MSC v.1500 32位(英特尔)]在Win32
  • 龙卷风== 3.2

回答

0

我已确定,这个问题是因为我关于使用GIT中击控制台视窗。当我使用常规命令提示符时,一切都按预期工作。我怀疑Git Bash正在赶上^ C并杀死这个进程。

-1

Windows有控制台不同的信号

尝试 CTRL + BREAK 或 按Ctrl + d