Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping a tornado application

Tags:

python

tornado

Let's take the hello world application in the Tornado home page:

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()

Is there a way, after the IOloop has been started and without stopping it, to essentially stop the application and start another one (on the same port or on another)?

I saw that I can add new application (listening on different ports) at runtime, but I do not know how I could stop existing ones.

like image 742
Roberto Avatar asked May 21 '14 08:05

Roberto


People also ask

How do you stop a tornado server?

1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID` , The Tornado Web Server Will shutdown after process all the request.

What is Tornado web application?

A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server. A minimal “hello world” example looks something like this: import asyncio import tornado.web class MainHandler(tornado. web.


1 Answers

Application.listen() method actually creates a HTTPServer and calls its listen() medthod. HTTPServer objects has stop() method which is probably what you need. But in order to do it you have to explicitly create HTTPServer object in your script.

server = HTTPServer(application)
server.listen(8888)
tornado.ioloop.IOLoop.instance().start()

#somewhere in your code
server.stop()
like image 168
Alex Shkop Avatar answered Sep 20 '22 07:09

Alex Shkop