小编use*_*746的帖子

FastAPI UploadFile 与 Flask 相比慢

我创建了一个端点,如下所示:

@app.post("/report/upload")
def create_upload_files(files: UploadFile = File(...)):
        try:
            with open(files.filename,'wb+') as wf:
                wf.write(file.file.read())
                wf.close()
        except Exception as e:
            return {"error": e.__str__()}
Run Code Online (Sandbox Code Playgroud)

它是用 uvicorn 启动的:

../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)

我正在执行一些测试,使用 Python 请求上传大约100 MB的文件,大约需要 128 秒:

../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)

我使用 Flask 通过 API 端点测试了相同的上传脚本,大约需要 0.5 秒:

f = open(sys.argv[1],"rb").read()
hex_convert = binascii.hexlify(f)
items = {"files": hex_convert.decode()}
start = time.time()
r = requests.post("http://192.168.0.90:5000/report/upload",files=items)
end = time.time() - start
print(end)
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?

python upload file-upload starlette fastapi

7
推荐指数
1
解决办法
9998
查看次数

标签 统计

fastapi ×1

file-upload ×1

python ×1

starlette ×1

upload ×1