小编Lin*_*SEO的帖子

当 uvicorn 工作人员> 1 时,uvicorn 和 fastAPI 与 pyinstaller 问题

我检查了 PyInstaller 和 FastAPI(超出最大递归深度)并且 Pyinstaller 编译的 Uvicorn 服务器无法正确启动

FastAPI 演示main.py

import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"hello": "world"}

if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=58000, reload=False)
Run Code Online (Sandbox Code Playgroud)

首先运行 pyinstallerpyinstaller -F main.py --clean并添加hidden_imports规范:

hidden_imports=[
                'uvicorn.logging',
                'uvicorn.loops',
                'uvicorn.loops.auto',
                'uvicorn.protocols',
                'uvicorn.protocols.http',
                'uvicorn.protocols.http.auto',
                'uvicorn.protocols.websockets',
                'uvicorn.protocols.websockets.auto',
                'uvicorn.lifespan',
                'uvicorn.lifespan.on',
            ]
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是当工人大于 1 时,应用程序必须是字符串:

WARNING: You must pass the application as an import string to enable 'reload' or 'workers'.

所以我改为:

if __name__ == '__main__': …
Run Code Online (Sandbox Code Playgroud)

python pyinstaller fastapi uvicorn

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

标签 统计

fastapi ×1

pyinstaller ×1

python ×1

uvicorn ×1