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.
What it is
Sanic is an asynchronous Python web framework built from the ground up with async/await support. It provides HTTP/1.1 and HTTP/2, WebSocket support, streaming responses, middleware, blueprints for modular routing, and auto-generated OpenAPI documentation. Sanic runs its own async event loop, eliminating the need for external ASGI servers in most deployments.
Sanic targets Python developers building high-performance APIs, microservices, and real-time applications. Its async-first design makes it suitable for I/O-bound workloads like API gateways, proxy services, and applications with many concurrent connections.
How it saves time or tokens
Sanic's built-in server means no separate ASGI/WSGI configuration. You define routes with decorators, add middleware, and run the server in one file. The auto-generated OpenAPI docs eliminate the need to maintain API documentation manually. Blueprints let you organize large applications into modular components without boilerplate.
How to use
- Install Sanic with
pip install sanic. - Define your application with route decorators and async handler functions.
- Run the server with
sanic app:appor by callingapp.run()in your script.
Example
from sanic import Sanic, json
app = Sanic('MyAPI')
@app.get('/api/items')
async def list_items(request):
return json([{'id': 1, 'name': 'Widget'}])
@app.post('/api/items')
async def create_item(request):
return json(request.json, status=201)
@app.websocket('/ws')
async def feed(request, ws):
while True:
data = await ws.recv()
await ws.send(f'Echo: {data}')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
Related on TokRepo
- API Tools — API frameworks and integration tools
- Coding AI Tools — Developer productivity tools
Common pitfalls
- Sanic is async-only. Blocking calls (synchronous database queries, file I/O without aiofiles) will block the event loop and degrade performance. Use async libraries for all I/O operations.
- Sanic runs its own server and manages worker processes internally. This differs from frameworks like Flask or FastAPI that rely on Uvicorn or Gunicorn. Check the Sanic deployment docs for production configuration.
- Sanic's ecosystem is smaller than Flask or FastAPI. Some third-party extensions may not have Sanic equivalents. Verify library compatibility before committing.
Frequently Asked Questions
Both are async Python frameworks. FastAPI focuses on type validation with Pydantic and automatic request/response serialization. Sanic focuses on raw performance and provides its own built-in server. FastAPI has a larger ecosystem; Sanic has been around longer and handles more connections per second in benchmarks.
Yes. Sanic has built-in WebSocket support. You define WebSocket routes with the @app.websocket decorator and use async recv/send methods. No additional packages required.
Yes. Sanic auto-generates OpenAPI 3.0 documentation from your route definitions. Access the docs at /docs by default. You can customize the documentation with docstrings and type annotations.
Sanic includes its own production-grade server with multi-worker support. Run with sanic app:app --workers 4 or configure workers programmatically. You can also put Nginx in front for TLS termination and static files.
Yes. Sanic supports request and response middleware via decorators. Middleware functions run before handlers (request middleware) or after handlers (response middleware). You can also use listeners for server lifecycle events.
Citations (3)
- Sanic GitHub— Sanic is an async Python web framework with native async/await, HTTP/2, and WebS…
- Sanic Documentation— Sanic server configuration and deployment
- Sanic Official Site— Async Python web framework comparison
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.