Python Tornado vs Flask

Posted

Python has two great web server libraries - Flask and Tornado.

Flask vs Tornado

Why Flask

Flask has a much easier to use routing functionality, where you just need a Python decorator @app.get(...).
In Tornado you need to set up a site router, and all these other things (or I didn’t do it right)

Why Tornado

Tornado is a non-blocking server, which is able to handle multiple requests at the same time.
Flask is blocking, and does not perform well when multiple requests are received

Porque les nos dos!?

Flask has the better routing engine than Tornado.
Tornado has the better server performance than Flask.

So… why not use them both!

Flask for the template engine (Routing + Jinja2 rendering) Tornado for the server

If we only want to use Flask for Jinja rendering, we could just implement Jinja in Tornado ourselves.


(a)sync

When we talk about servers, two terms often pop up - synchronous and asynchronous.

A synchronous server performs each operation in order.
An asynchronous server performs its operations as they please.

Flask’s inbuilt server is synchronous (blocking); meaning that newer requests not performed until the previous request is completed.
Tornado’s inbuilt server is asynchronous (non-blocking); meaning that several requests can be handled at the same time.

Hence, where a website might load many files (js, css, images, etc) - Tornado wins here.

Although…
Flask’s inbuilt server is not meant to be used for production environments, only development / testing. They recommend setting up a WSGI container, like Gunicorn.

More posts

z-index

Negative z-indexes - Argh!

Posted

HTML Script Tags

A handy diagram to explain the functionality of async and defer in <script> tags

Posted