FastAPI: combining BackgroundTasks and UploadFile gives SyntaxError: non-default argument follows default argument
FastAPI: combining BackgroundTasks and UploadFile gives SyntaxError: non-default argument follows default argument
我定义端点如下:
from fastapi import FastAPI, Request, Response, File, UploadFile, BackgroundTasks
@app.post("/api/upload_file", response_class=JSONResponse)
async def upload_file(file: UploadFile = File(...), background_tasks: BackgroundTasks):
background_tasks.add_task(compute_secondary_structure_data, file)
...
所以,基本上,我收到了一个用户上传的文件,我想在后台用它做一些事情,而我已经向用户发送了响应(因此,需要 BackgroundTask
).
但我得到以下信息:SyntaxError: non-default argument follows default argument
在 FastAPI 中实现我想要的并将这两个参数结合起来的最佳方法是什么?有没有办法为后台任务参数添加默认值?或者删除文件上传的那个?
谢谢
您可以将 background_tasks
作为 关键字参数 作为
@app.post("/api/upload_file", response_class=JSONResponse)
async def upload_file(
file: UploadFile = File(...),
<b>background_tasks: BackgroundTasks = BackgroundTasks()</b>
):
background_tasks.add_task(compute_secondary_structure_data, file)
...
我定义端点如下:
from fastapi import FastAPI, Request, Response, File, UploadFile, BackgroundTasks
@app.post("/api/upload_file", response_class=JSONResponse)
async def upload_file(file: UploadFile = File(...), background_tasks: BackgroundTasks):
background_tasks.add_task(compute_secondary_structure_data, file)
...
所以,基本上,我收到了一个用户上传的文件,我想在后台用它做一些事情,而我已经向用户发送了响应(因此,需要 BackgroundTask
).
但我得到以下信息:SyntaxError: non-default argument follows default argument
在 FastAPI 中实现我想要的并将这两个参数结合起来的最佳方法是什么?有没有办法为后台任务参数添加默认值?或者删除文件上传的那个?
谢谢
您可以将 background_tasks
作为 关键字参数 作为
@app.post("/api/upload_file", response_class=JSONResponse)
async def upload_file(
file: UploadFile = File(...),
<b>background_tasks: BackgroundTasks = BackgroundTasks()</b>
):
background_tasks.add_task(compute_secondary_structure_data, file)
...