As far as I can tell Tornado is a server and a framework in one. It seems to me that using Flask and Tornado together is like adding another abstraction layer (more overhead). Why do people use Flask and Tornado together, what are the advantages?
4 Answers
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 and templates (undocumented) 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.

- 4,799
- 3
- 38
- 59

- 17,331
- 4
- 53
- 56
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 WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from yourapplication import app
http_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.

- 5,627
- 25
- 37
-
6Unless I'm mistaken, blocking activity within the Flask app will still disrupt and hang the Tornado reactor, right? – DeaconDesperado Apr 09 '13 at 18:44
-
3Yep, Flask will block. But that doesn't change if you run it under a different server. – Cole Maclean Apr 12 '13 at 05:12
-
2I abandoned Flask and UWSGI completely and just use Mako Templates. It took ~30 lines of code to incorporate Mako, so now I have routing, handlers and templating (that's pretty much what a framework is). My app is managed by supervisord and it's all done and running fine, no room for Flask. – 3k- Apr 04 '15 at 19:21
instead of using Apache as your server, you'll use Tornado (of course as blocking server due to the synchronous nature of WSGI).

- 4,252
- 11
- 42
- 65
-
3But if that is true, what would be the point of using Tornado instead of the flask web server, which is also blocking? Are you sure about Tornado being blocking in this case? – Zelphir Kaltstahl Nov 07 '16 at 14:54
Use each one for what is better at, but take into account that the mix don't always get the performance gains you are looking for.
This benchmark, https://gist.github.com/andreif/6088558, for example, question what combination of frameworks is faster, you must adapt the example to your main use case. On this example, flask+tornado don't deliver the expected performance gains.

- 5,961
- 3
- 22
- 17