Why does ctrl + c not stop tornado server on windows ?
This code is not executed: print 'get sig:%d' % signum
import signal
import tornado
import tornado.web
import tornado.httpserver
class DefaultHandler(tornado.web.RequestHandler):
def get(self):
self.set_status(200, 'OK')
self.write('hello guest')
def post(self):
self.get()
class Receiver(object):
def __init__(self, address=None, port=8100, gzip=False):
if not address:
address = '0.0.0.0'
self.address = address
self.port = port
self.gzip = gzip
self.http_server = None
def start(self):
settings = dict(
)
application = tornado.web.Application([
(r"/", DefaultHandler),],
**settings)
self.http_server = tornado.httpserver.HTTPServer(application, decompress_request=self.gzip)
self.http_server.listen(self.port)
tornado.ioloop.IOLoop.instance().start()
def shutdown(self):
if self.http_server is not None:
self.http_server.stop()
tornado.ioloop.IOLoop.instance().stop()
if __name__ == '__main__':
receiver = Receiver(port=8901)
def sig_handler(signum, frame):
print 'get sig:%d' % signum
receiver.shutdown()
signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)
receiver.start()
My solution:
only main thread can handle signal, so use thread to start receiver and let main thread do some fake work to keep alive
threading.Thread(target=receiver.start).start()
while 1:
try:
time.sleep(2)
except:
break
I wrote the more detailed answer here: https://stackoverflow.com/a/52941752/207661
In short, you need to install Ctrl+C handler for Windows manually.
Below are handy functions which will detect Windows and install custom handler for Ctrl+C in console:
You can use above like this:
Signal handlers are a special environment. You have to be careful what you do in one because you don't know the state of the functions that were interrupted by the signal. In tornado, only one function is guaranteed safe to call from inside a signal handler:
IOLoop.add_callback_from_signal
. Instead of callingshutdown()
directly, useadd_callback_from_signal
to schedule it: