Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the tornado web server with ctrl+c?

I am new to tornado web server. When I start the tornado web server using python main_tornado.py It is working. Please see the below code.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

When I stop the server using CTRL+C it gave the following error.

    ^CTraceback (most recent call last):
  File "main_tornado.py", line 19, in <module>
    tornado.ioloop.IOLoop.instance().start()
  File "/home/nyros/Desktop/NewWeb/venv/lib/python3.2/site-packages/tornado/ioloop.py", line 301, in start
    event_pairs = self._impl.poll(poll_timeout)
KeyboardInterrupt

Please solve my problem. Thanks..

like image 954
dhana Avatar asked Jun 14 '13 05:06

dhana


2 Answers

You can stop Tornado main loop with tornado.ioloop.IOLoop.instance().stop(). To have this method called after passing signal with Ctrl+C you can periodically check global flag to test if main loop should end and register handler for SIGINT signal which will change value of this flag:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import signal
import logging

import tornado.ioloop
import tornado.web
import tornado.options


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


class MyApplication(tornado.web.Application):
    is_closing = False

    def signal_handler(self, signum, frame):
        logging.info('exiting...')
        self.is_closing = True

    def try_exit(self):
        if self.is_closing:
            # clean up here
            tornado.ioloop.IOLoop.instance().stop()
            logging.info('exit success')


application = MyApplication([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    tornado.options.parse_command_line()
    signal.signal(signal.SIGINT, application.signal_handler)
    application.listen(8888)
    tornado.ioloop.PeriodicCallback(application.try_exit, 100).start()
    tornado.ioloop.IOLoop.instance().start()

Output:

$ python test.py 
[I 181209 22:13:43 web:2162] 200 GET / (127.0.0.1) 0.92ms
^C[I 181209 22:13:45 test:21] exiting...
[I 181209 22:13:45 test:28] exit success

UPDATE

I've just saw in question Tornado long polling requests this simple solution:

try:
    tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
    tornado.ioloop.IOLoop.instance().stop()

Obviously, this is a less safe way.


UPDATE

Edited the code to remove use of global.

like image 63
Nykakin Avatar answered Oct 04 '22 15:10

Nykakin


You can simply stop the Tornado ioloop from a signal handler. It should be safe thanks to add_callback_from_signal() method, the event loop will exit nicely, finishing any eventually concurrently running task.

import tornado.ioloop
import tornado.web
import signal

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

def sig_exit(signum, frame):
    tornado.ioloop.IOLoop.instance().add_callback_from_signal(do_stop)

def do_stop(signum, frame):
    tornado.ioloop.IOLoop.instance().stop()

if __name__ == "__main__":
    application.listen(8888)
    signal.signal(signal.SIGINT, sig_exit)
    tornado.ioloop.IOLoop.instance().start()
like image 21
Ales Teska Avatar answered Oct 04 '22 14:10

Ales Teska