A Beginner’s Guide to Python Tornado

The web is a big place now. We need to support thousands of clients at a time and here comes Tornado. Tornado is a Python web framework and asynchronous network library, originally developed at FriendFreed.

Tornado uses non-blocking network-io. Due to this, it can handle thousands of active server connections. It is a saviour for applications where long polling and a large number of active connections are maintained.

Tornado is not like most Python frameworks. It’s not based on WSGI while it supports some features of WSGI using module `tornado.wsgi`. It uses an event loop design that makes Tornado request execution faster.

What is Synchronous Program?

Application performance depends on how efficiently application uses CPU cycles, that’s why blocking statements/calls must be taken seriously. Consider password hashing functions like bcrypt, which by design use hundreds of milliseconds of CPU time, far more than a typical network or disk access. As the CPU is not idle, there is no need to go for asynchronous functions.

A function can be blocking in one and non-blocking in others. In the context of Tornado, we generally consider blocking due to network I/O and disk, although all kinds of blocking need to be minimized.

What is Asynchronous Program?

Means it can’t do computation-centric tasks parallely.

2) I/O concurrency:

It can handover IO tasks to the operating system and continue to the next task to achieve parallelism.

3) epoll/ kqueue:

Underline system-related construct that allows an application to get events on a file descriptor or I/O specific tasks.

4) Event loop:

It uses epoll or kqueue to check if any event has happened and executes callback that is waiting for those network events.

Asynchronous vs Synchronous Web Framework:

On the other hand, in Asynchronous framework, like Node.js, there is a single-threaded model, so very less overhead, but it has complexity.

Let’s imagine thousands of requests coming through and a server uses event loop and callback, now until request gets processed, it has to efficiently store and manage the state of that request to map callback result to the actual client.

Node.js vs Tornado

  • Node.js has one big advantage that all of its l libraries are Async. In Python, there are lots of available packages but very few of them are asynchronous
  • As Node.js is JavaScript runtime, and we can use JS for both front and back-end, developers can keep only one codebase and share the same utility library
  • Google’s V8 engine makes Node.js faster than Tornado. But a lot of Python libraries are written in C and can be faster alternatives.

A Simple ‘Hello World’ Example

Note: This example does not use any asynchronous feature.

Using AsyncHTTPClient module, we can do REST call asynchronously.

As you can see `yield http_client.fetch(url)` will run as a coroutine.

Complex Example of Tornado Async

WebSockets Using Tornado:

One can use a WebSocket client application to connect to the server, message can be any integer. After processing, the client receives the result if that integer is prime or not.

Here is one more example about actual async features of Tornado, many will find it similar to Golang’s Goroutine and channels.

In this example, we can start worker(s) and they will listen to the ‘ tornado.queue’ , itself. This queue is asynchronous and very similar to the asyncio package.

Conclusion

2) Due to a single thread per core model and event loop, it can manage thousands of active client connections.

3) Many say Django is too big, Flask is too small and Tornado is just right:)

Originally published at https://www.velotio.com.

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store