Search code examples
tornado

Tornado: Application or HTTPServer


Various Tornado documents show using either tornado.web.Application directly:

application = tornado.web.Application(endpoints, **settings)
application.listen(8888)
tornado.ioloop.IOLoop.current().start()

or via HTTPServer:

application = tornado.web.Application(endpoints, **settings)
server = tornado.httpserver.HTTPServer(application)
server.listen(8888)
tornado.ioloop.IOLoop.current().start()

What's the difference? The documentation hasn't cleared it up to me.


Solution

  • Application.listen is shorthand for constructing an HTTPServer and calling listen on it. This is convenient because you probably need to import the Application class anyway and won't otherwise need to touch the HTTPServer.

    The only time you would need to construct an HTTPServer by hand is if you need to call some methods other than listen on it (you can even pass HTTPServer constructor arguments through Application.listen, so you can get HTTPS that way). You would need to do this if you're using multi-process mode, or using something unusual with HTTPServer.add_sockets.