Configs2026年4月12日·1 分钟阅读

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
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

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()
介绍

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产