Why use Tornado and Flask together? Why use Tornado and Flask together? python python

Why use Tornado and Flask together?


According to this question it is because Flask is blocking and Tornado is non-blocking.

If one uses Tornado as a WSGI server and Flask for url routing + templates there shouldn't be any overhead. With this approach you aren't using Flask's web server, so there isn't really an extra layer of abstraction.

However, if one is using Flask just for the templates they could use Tornado with Jinja2 which is the template engine that Flask uses.


I always thought using Flask & Tornado together was stupid, but it actually does make sense. It adds complexity though; my preference would be to just use Tornado, but if you're attached to Flask, then this setup works.

Flask is (reportedly) very nice to use, and simpler than Tornado. However, Flask requires a WSGI server for production (or FCGI, but that's more complicated). Tornado is pretty simple to setup as a WSGI server:

from tornado.wsgi import WSGIContainerfrom tornado.httpserver import HTTPServerfrom tornado.ioloop import IOLoopfrom yourapplication import apphttp_server = HTTPServer(WSGIContainer(app))http_server.listen(5000)IOLoop.instance().start()

In this situation the developer just needs to worry about the Flask app. Tornado just acts as a server.

It's also possible to handle some requests (for example, websockets, which don't play nice with WSGI) using Tornado, and still do the majority of your work in Flask. In theory, you'll get the simplicity of Flask with the async performance of Tornado.


instead of using Apache as your server, you'll use Tornado (of course as blocking server due to the synchronous nature of WSGI).