Learn TORNADO with Real Code Examples

Updated Nov 25, 2025

Explain

Tornado uses a non-blocking, single-threaded event loop for handling concurrent connections efficiently.

It combines a web framework with a scalable networking library, suitable for real-time apps.

Supports WebSockets, long polling, and streaming responses natively.

Provides asynchronous request handling using Python's `asyncio` or Tornado's own I/O loop.

Commonly used for chat applications, live dashboards, and APIs requiring high concurrency.

Core Features

RequestHandler classes for defining endpoints

Async I/O via `async def` or `gen.coroutine`

Built-in support for WebSockets

Streaming responses and long-lived connections

Routing via `tornado.web.Application`

Basic Concepts Overview

IOLoop - core event loop

Application - main server object

RequestHandler - class to handle HTTP requests

WebSocketHandler - class for WebSocket connections

Async coroutines - for non-blocking operations

Project Structure

app.py - main server file

handlers/ - RequestHandler classes

templates/ - HTML templates if used

static/ - static files (CSS, JS, images)

utils/ - helper modules and services

Building Workflow

Import Tornado modules (`tornado.web`, `tornado.ioloop`)

Define RequestHandler classes for endpoints

Create Application instance with route mappings

Start IOLoop with `app.listen(port)` and `IOLoop.current().start()`

Use `async def` or `@gen.coroutine` for async operations

Difficulty Use Cases

Beginner: single GET endpoint

Intermediate: CRUD API with async DB calls

Advanced: WebSocket server

Expert: high-concurrency microservices

Auditor: optimize event loop performance

Comparisons

Tornado vs Flask: Tornado supports async and WebSockets, Flask is WSGI-based

Tornado vs Django: Tornado is async and lightweight, Django is feature-rich

Tornado vs FastAPI: Both async, Tornado includes low-level I/O, FastAPI uses Starlette for async web

Tornado vs Node.js frameworks: Tornado is Python-based, async event-loop similar to Node.js

Tornado vs Koa.js: Koa is Node.js minimal async, Tornado is Python async with networking library

Versioning Timeline

2009 – Tornado initial release by FriendFeed team

2010 – Open-sourced and adopted by Facebook

2012–2015 – Async features and WebSocket support improved

2016–2019 – Python 3 support and asyncio integration

2020–2025 – Continuous maintenance and minor optimizations

Glossary

IOLoop - core event loop

RequestHandler - handles HTTP requests

WebSocketHandler - handles WebSocket connections

Application - server object with routes

Coroutine - async function for non-blocking I/O