Skip to main content

Tornado architecture

Tornado, a Python web framework and asynchronous networking library, is a powerful tool for building high-performance web services. In this article, we will delve into the architecture of Tornado to better understand its workings.

1. Overview of Tornado Architecture

Tornado's architecture is built to handle thousands of simultaneous connections, making it ideal for real-time web services. It uses a non-blocking network I/O and can scale to tens of thousands of open connections, making it perfect for long polling, WebSockets, and other applications that require a long-lived connection to each user.

1.1 Non-blocking I/O

Tornado is based on non-blocking I/O (Input/Output) operations. Traditional web servers use blocking I/O, which means they wait for data before continuing their operations. Non-blocking I/O, on the other hand, allows Tornado to serve other requests while waiting for data, resulting in a highly efficient use of resources.

2. Event Loop

The event loop is at the core of Tornado's architecture. It's a programming construct that waits for and dispatches events or messages in a program. In Tornado, it's provided by the IOLoop class. The IOLoop is responsible for handling all I/O events and dispatching them to their respective Handlers.

2.1 Handlers

Handlers in Tornado are responsible for processing requests and producing responses. When a request arrives, a handler is created, and the appropriate method on that handler is invoked based on the type of HTTP request.

3. Tornado’s Asynchronous Libraries

Tornado provides support for third-party libraries, allowing them to operate asynchronously. These libraries include AsyncHTTPClient (for non-blocking HTTP requests), AsyncTestCase and gen_test (for asynchronous unit tests), and many others.

4. Application and RequestHandler

In Tornado, an Application is a collection of request handlers that make up a web application. The RequestHandler does the work of handling the request, forming the response, and deciding how to interact with the client.

4.1 Application

The Application class is the heart of any Tornado web application. It's where your URL routes and settings are defined.

4.2 RequestHandler

The RequestHandler class is where you define how to respond to any given request. Each HTTP verb (GET, POST, PUT, DELETE) corresponds to a method on your RequestHandler.

5. Templates and UI Modules

Tornado also provides a template language. This allows you to generate HTML from Python code, which makes building dynamic web pages a breeze. UI Modules are a feature that allows for reusable components (like headers and footers) in your templates.

6. Conclusion

That's a brief overview of Tornado's architecture. Tornado uses a non-blocking network I/O and an event loop to handle thousands of simultaneous connections. It's a powerful framework that's perfect for real-time web services.

Stay curious and keep exploring Tornado's amazing capabilities!