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

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.

SC
Script Depot · Community
快速使用

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

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

pip install sanic
from sanic import Sanic, json

app = Sanic("TokRepo")

@app.get("/api/assets")
async def list_assets(request):
    return json([{"repo": "react", "stars": 230000}])

@app.post("/api/assets")
async def create_asset(request):
    return json(request.json, status=201)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, auto_reload=True)
介绍

Sanic is an async Python web framework built for speed. It was one of the first Python frameworks to embrace async/await natively (Python 3.5+), using uvloop for higher throughput. Features built-in server, WebSocket support, streaming, middleware, and auto-generated OpenAPI docs.

What Sanic Does

  • Async nativeasync def handlers throughout
  • Built-in server — no external WSGI/ASGI server needed
  • WebSocket — native support
  • Streaming — request and response streaming
  • Middleware — request/response lifecycle hooks
  • Blueprints — modular route grouping
  • OpenAPI — auto-generated API docs
  • Signals — event-based hooks
  • Background tasks — via app.add_task()

Comparison

Framework Server Speed
Sanic Built-in (uvloop) Very fast
FastAPI Uvicorn Very fast
Flask Gunicorn Medium
Tornado Built-in Fast

常见问题 FAQ

Q: Sanic vs FastAPI? A: FastAPI 有 Pydantic 验证 + 自动 OpenAPI + 更大社区;Sanic 有内置服务器、更灵活的中间件、更早的 async 支持。新 API 项目多选 FastAPI。

来源与致谢 Sources

讨论

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

相关资产