Sanic
Sanic is an asynchronous Python 3.10+ web server and web framework designed for high performance. It leverages Python's `async/await` syntax and optionally `uvloop` for blazing-fast I/O. Sanic is ASGI compliant, allowing flexible deployment, and offers a Flask-like API for rapid development. The project is actively maintained by the community, with a frequent release cadence, often monthly, following a YY.MM.PATCH versioning scheme. The current version is 25.12.0.
Warnings
- breaking Sanic requires explicit response objects (e.g., `text()`, `json()`). Unlike frameworks like Flask, returning raw strings or dictionaries from handlers is not supported and will result in errors, as Sanic aims to avoid implicit conversions for performance.
- breaking The functionality and parsing of `Request.accept` changed significantly. Relying on its equality operations may produce incorrect results. The `match()` method is now the preferred way to interact with `Request.accept`.
- breaking Response cookies are no longer dict-like objects. Direct dictionary methods (e.g., `cookie['key'] = value`) for `response.cookies` were removed.
- breaking Duplicate route names are no longer allowed. Registering multiple routes with the same name will raise a `sanic.exceptions.ServerError`.
- deprecated The `sanic.worker.GunicornWorker` class has been removed. Running Sanic with Gunicorn should now be done via `uvicorn` as an ASGI application.
- gotcha `uvloop` is Unix-specific. If `uvloop` is enabled (which is the default on supported platforms), Sanic applications may not run or perform optimally on Windows. You can explicitly disable `uvloop` via an environment variable.
Install
-
pip install sanic -
pip install "sanic[ext]"
Imports
- Sanic
from sanic import Sanic
- Response types
from sanic.response import text, json
- Request
async def handler(request: Request):
Quickstart
from sanic import Sanic
from sanic.response import text
app = Sanic("MyHelloWorldApp")
@app.get("/")
async def hello_world(request):
return text("Hello, world.")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)