Tornado: The Complete Guide to Python's Async Web Framework
What is Tornado?
Tornado is a powerful asynchronous web server and web framework for Python, specifically designed to handle a large number of concurrent connections. Built by the FriendFeed team (later acquired by Facebook), Tornado was one of the pioneers in asynchronous programming for Python.
The core philosophy of Tornado is based on non-blocking I/O and an event-driven architecture. This allows a single process to serve thousands of connections simultaneously, making it an ideal choice for building high-traffic web applications, real-time chat systems, and API services.
Key Features & Benefits of Tornado
Asynchronous Architecture
Tornado uses an event-driven programming model that can handle many requests within a single thread. This significantly reduces the overhead of context switching and memory management.
Built-in HTTP Server
Unlike many other frameworks, Tornado includes its own HTTP server, eliminating the need for additional components like WSGI to deploy your applications.
WebSocket Support
Tornado provides native support for WebSocket connections, making it an excellent choice for building interactive, real-time applications.
asyncio Compatibility
Starting with version 5.0, Tornado is fully compatible with asyncio, allowing you to use it alongside other asynchronous Python libraries.
Flexible Routing System
Tornado offers a powerful URL routing system with support for regular expressions and parameters.
Built-in Template Engine
The framework includes its own template engine for generating HTML pages with dynamic content.
Installation & Environment Setup
Installing Tornado
pip install tornado
For additional functionality, you can install extra packages:
# For MongoDB support
pip install motor
# For PostgreSQL support
pip install asyncpg
# For Redis support
pip install aioredis
Project Structure
Recommended Tornado project structure:
project/
├── app.py # Main application file
├── handlers/ # Request handlers
│ ├── __init__.py
│ ├── base.py
│ ├── auth.py
│ └── api.py
├── models/ # Data models
│ ├── __init__.py
│ └── user.py
├── templates/ # HTML templates
│ ├── base.html
│ ├── index.html
│ └── error.html
├── static/ # Static files
│ ├── css/
│ ├── js/
│ └── images/
├── config/ # Configuration files
│ ├── __init__.py
│ └── settings.py
└── requirements.txt # Project dependencies
Building Your First Application
Basic Example
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Welcome to Tornado!")
class AboutHandler(tornado.web.RequestHandler):
def get(self):
self.write("About Us Page")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/about", AboutHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
print("Server running on http://localhost:8888")
tornado.ioloop.IOLoop.current().start()
Configuring the Application with Parameters
import tornado.web
import tornado.ioloop
import os
class Application(tornado.web.Application):
def __init__(self):
handlers Also in library
Armadillo for C++: Complete Guide with Examples
Working with Apache Kafka in Go (Golang): Libraries and Examples
Prometheus Metrics in TypeScript: Complete Guide to client.js
Parsing HTML in Rust: Libraries and Examples for Web Scraping