Sanic是一个类似Flask的Python 3.5+ Web服务器,它的写入速度非常快。除了Flask之外,Sanic还支持异步请求处理程序。这意味着你可以使用Python 3.5中新的闪亮的异步/等待语法,使你的代码非阻塞和快速。
在上一篇博文中已经讲到,如何在Sanic中使用Cookie,接下来将介绍一下Sanic的流的使用:
请求流式传输
Sanic允许通过流获取请求数据,如下所示,当请求结束时,request.stream.get()
返回为None
,只有post
、put
和patch decorator
拥有流参数:
from sanic.response import stream
@app.post("/post_stream",stream=True)
async def post_stream(request):
async def streaming(response):
while