1. sanic 是基于python 3.7 的web服务器和web框架,是一个无阻塞的框架,旨在提高性能。
2. 特性
内置极速web server
生产准备就绪
支持ASGI
简单直观的API涉及
3. 安装
pip install sanic
4. hello world
#hello.py
from sanic import Sanic
from sanic.response import text
app = Sanic("hello")
@app.get("/")
async def hello(request):
return text("hello world! ")
每一个请求响应函数都可以用同步或异步方式进行声明,除非有明确的需求,否则建议使用异步方式声明。
request是响应函数的第一个参数。
必须使用response 的类作为响应类型,比如上面是text,sanic 要求明确调用方式。
5. 运行
sanic hello.app (sanic 会自动将app 找到对应的py文件)
其他框架带有内置开发服务器,但一般都明确只用于开发,而sanic 是生产就绪,可以直接在生产环境中运行。