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?
A function blocks, performs its computation and returns, once done . A function may block for many reasons: network I/O, disk I/O, mutexes, etc.
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?
1) Single-threaded architecture:
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:
In case of synchronous model, each request or task is transferred to thread or routing and as it finishes, the result is handed over to the caller. Here managing things are easy but creating new threads is too much overhead.
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
Most of these comparison points are tied to actual programming language and not the framework:
- 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
- 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
Please have a look at Asynchronous Request handler.
WebSockets Using Tornado:
Tornado has built-in package for WebSockets that can be easily used with coroutines to achieve concurrency, here is one example:
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.
1) Asynchronous frameworks are not much of use when most of the computations are CPU centric and not I/O.
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:)