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.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install 412ffd8f-3634-11f1-9bc6-00163e2b0d79 --target codexÀ exécuter après confirmation du plan en dry-run.
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.
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Fil de discussion
Actifs similaires
Sanic — Async Python Web Framework Built for Speed
Sanic is an async Python web framework built for speed. Native async/await from the ground up, HTTP/1.1 and HTTP/2, WebSocket, streaming, and auto-generated API docs. Designed to be fast, flexible, and easy to use.
Scrapy — Fast High-Level Web Crawling Framework for Python
Scrapy is the most battle-tested web scraping framework for Python. It handles concurrency, retries, throttling, cookies, and export pipelines — letting you write spiders that scale from one page to millions with the same code.
Litestar — High-Performance Python ASGI Web Framework
Litestar is a powerful, flexible, and opinionated ASGI web framework for building modern Python APIs with class-based controllers, dependency injection, and automatic OpenAPI docs.
Ktor — Async Web Framework for Kotlin by JetBrains
Ktor is a framework for quickly creating connected applications in Kotlin with minimal effort. Built by JetBrains, it supports coroutines natively, runs on multiple engines (Netty, CIO, Jetty), and works for server + client HTTP in Kotlin Multiplatform.