Man*_*are 2 python api asynchronous sanic
我正在尝试处理用户上传的文件。但是,我希望用户在上传完成后得到响应并终止连接,但继续处理文件。我的代码看起来像这样:
@app.route("/upload", methods=['POST'])
async def upload(request):
try:
file = request.files.get("file")
except Exception as e:
return json({"Received": False})
await process(file)
return response.text("File has been uploaded successfully")
async def process(file):
""" Processing File and generate data"""
...
result = await foo(data)
Run Code Online (Sandbox Code Playgroud)
在当前框架中,用户必须等待该过程完成。我希望用户得到这样的回复:
“文件已上传成功”
并终止连接但仍继续处理(文件)。
我认为你这里有两种可能的解决方案。第一个会稍微偏离您的要求,因为它不会终止连接。不过,我认为这是一个可行的解决方案,所以我也提供它。如果不是为了您的利益,也许是为了其他有类似需求的人。
此选项是流式响应,您可以立即发回第一部分,然后根据需要继续处理响应。现在,这显然在某种程度上取决于我们正在讨论的处理量。如果它有点长(选择任意时间,60 秒),那么这个解决方案可能不起作用。
import asyncio
from functools import partial
from sanic import Sanic
from sanic.response import json, stream
app = Sanic(__name__)
async def process(file, response):
print(file, flush=True)
await response.protocol.push_data(b"0x0")
await response.write("File has been uploaded successfully\n")
await asyncio.sleep(2)
await response.write("33% processed\n")
await asyncio.sleep(2)
await response.write("66% processed\n")
await asyncio.sleep(2)
await response.write("100% processed\n")
@app.post("/")
async def upload(request):
try:
file = request.files.get("file")
except Exception:
return json({"Received": False})
return stream(partial(process, file), chunked=False)
Run Code Online (Sandbox Code Playgroud)
我相信这更符合您的需求。立即响应(关闭连接)。我们使用processing并将其推到后台add_task。
import asyncio
from functools import partial
from sanic import Sanic
from sanic.response import json, text
app = Sanic(__name__)
async def process(file,):
await asyncio.sleep(3)
print(f"{file=}", flush=True)
@app.post("/")
async def upload(request):
try:
file = request.files.get("file")
except Exception:
return json({"Received": False})
request.app.add_task(process(file))
return text("File has been uploaded successfully")
Run Code Online (Sandbox Code Playgroud)
作为另一种选择,这是我放在一起的一个示例,其中处理程序将任务推送到队列,并且有一个单独的“工作人员”正在排空队列并执行任务。