Tornado — Python Async Web Framework and Networking Library
Tornado is a Python web framework and asynchronous networking library originally developed at FriendFeed (acquired by Facebook). Non-blocking I/O, WebSockets, long polling, and thousands of simultaneous connections. One of the earliest async Python web frameworks.
What it is
Tornado is a Python web framework and asynchronous networking library originally developed at FriendFeed (acquired by Facebook in 2009). It was one of the earliest non-blocking I/O frameworks for Python, designed for applications that need to maintain thousands of simultaneous connections. Tornado provides its own HTTP server, WebSocket support, long polling, and server-sent events.
Tornado targets Python developers building real-time applications like chat systems, live dashboards, and streaming APIs. While newer frameworks like FastAPI have become more popular for REST APIs, Tornado remains relevant for WebSocket-heavy and long-lived connection workloads.
How it saves time or tokens
Tornado includes an HTTP server, so you do not need a separate WSGI/ASGI server for deployment. Its request handler pattern is straightforward: define a class with get/post methods and register URL patterns. WebSocket support is built in with a simple handler class, eliminating the need for additional libraries.
How to use
- Install Tornado:
pip install tornado. - Define request handler classes with HTTP method handlers (get, post, etc.).
- Create an Application with URL routing and start the IOLoop.
Example
import tornado.ioloop
import tornado.web
import tornado.websocket
import json
class ApiHandler(tornado.web.RequestHandler):
def get(self):
self.write(json.dumps({'status': 'ok'}))
class ChatSocket(tornado.websocket.WebSocketHandler):
connections = set()
def open(self):
self.connections.add(self)
def on_message(self, msg):
for conn in self.connections:
conn.write_message(msg)
def on_close(self):
self.connections.discard(self)
app = tornado.web.Application([
(r'/api', ApiHandler),
(r'/ws', ChatSocket),
])
if __name__ == '__main__':
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Related on TokRepo
- AI Tools for API -- explore API frameworks and tools for building backend services
- AI Tools for Coding -- discover Python development tools and workflows
Common pitfalls
- Tornado's callback-based async style predates Python's async/await; newer code should use native coroutines with
async deffor readability. - Tornado runs a single-threaded event loop by default; CPU-bound work blocks all connections. Offload heavy computation to thread pools.
- Tornado's templating engine is basic compared to Jinja2; most projects integrate Jinja2 for complex HTML rendering.
Frequently Asked Questions
FastAPI is newer, uses Python type hints for automatic validation and documentation, and integrates with ASGI servers. Tornado has built-in WebSocket support and its own HTTP server. FastAPI is better for REST APIs; Tornado excels at long-lived connections.
Yes. Since Python 3.5, Tornado supports native async/await syntax. Modern Tornado code should use async def coroutines instead of the older callback or gen.coroutine patterns.
Yes. Tornado's non-blocking I/O loop handles thousands of simultaneous connections on a single thread. It was designed specifically for this use case at FriendFeed.
Yes. Tornado continues to receive updates, though the release cadence is slower than newer frameworks. It remains a stable choice for production WebSocket applications.
Tornado has its own HTTP server and event loop, separate from the ASGI ecosystem. You can bridge Tornado and ASGI using adapters, but they are different architectures.
Citations (3)
- Tornado GitHub— Tornado is a Python async web framework from FriendFeed
- Tornado Docs— Tornado documentation and user guide
- Python Docs— Python asyncio event loop documentation
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.