2014-02-16 22 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