ConfigsApr 12, 2026·1 min read

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.

AI
AI Open Source · Community
Quick Use

Use it first, then decide how deep to go

This block should tell both the user and the agent what to copy, install, and apply first.

pip install tornado
import tornado.ioloop
import tornado.web
import json

class AssetsHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(json.dumps([{"repo": "react", "stars": 230000}]))

    def post(self):
        data = json.loads(self.request.body)
        self.set_status(201)
        self.write(json.dumps(data))

class WebSocketHandler(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/assets", AssetsHandler),
    (r"/ws", WebSocketHandler),
])

if __name__ == "__main__":
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Intro

Tornado is a Python web framework and asynchronous networking library originally developed at FriendFeed (acquired by Facebook in 2009). One of the earliest non-blocking I/O frameworks for Python, Tornado is designed for long-lived connections like WebSockets, long polling, and server-sent events. Used at Facebook, Quora, and other high-concurrency applications.

What Tornado Does

  • Non-blocking I/O — event loop based
  • Web framework — routing, templates, auth, XSRF
  • WebSocket — native support
  • HTTP client — async HTTP requests
  • Coroutinesasync def / await (modern) or gen.coroutine (legacy)
  • asyncio compatible — integrates with Python asyncio loop

Comparison

Framework Async Model WebSocket Era
Tornado IOLoop (asyncio) Built-in 2009
FastAPI ASGI (asyncio) Via Starlette 2018
Sanic uvloop Built-in 2016
aiohttp asyncio Built-in 2014

常见问题 FAQ

Q: 新项目还用 Tornado 吗? A: 新项目推荐 FastAPI 或 Starlette。Tornado 主要维护老项目或需要自定义 IOLoop 的场景。

来源与致谢 Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets