Why use Tornado and Flask together?
Question:
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?
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.
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.
instead of using Apache as your server, you’ll use Tornado (of course as blocking server due to the synchronous nature of WSGI).
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.
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?
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.
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.
instead of using Apache as your server, you’ll use Tornado (of course as blocking server due to the synchronous nature of WSGI).
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.