Search code examples
pythonwindowspython-2.7flasktornado

flask-tornado CTRL+C termination in windows


Below code:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop  


def start(app, port=8080):  
      http_server = HTTPServer(WSGIContainer(app))
      http_server.listen(port)
      try:
          IOLoop.instance().start()
      except KeyboardInterrupt:
          print "stop"
          IOLoop.instance().stop()

I want to stop tornado server using CTRL+C or CTRL+PauseBreak in Windows, but Ctrl+C does not stop it in CMD.

Ctrl+PauseBreak does stop CMD and terminate python.exe, but does not show "stop".

How to enter KeyboardInterrupt in windows?


Solution

  • On Windows, the select() function (which IOLoop uses internally) is not interruptible (http://www.velocityreviews.com/forums/t722370-windows-select-select-timeout-and-keyboardinterrupt.html). The easiest workaround is to start a PeriodicCallback (it doesn't have to do anything; just an empty function); when the callback is triggered the IOLoop will wake up and the KeyboardInterrupt exception will be raised.