我正在使用具有多个处理器(5 个进程)的 UVICORN 运行 python FastAPI,它从代码中运行顺利,但是当我尝试从 pyinstaller 生成 exe 并尝试运行该文件时,它显示错误。
文件名:main.py
import multiprocessing
import os
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
multiprocessing.freeze_support()
print("Running the instance")
uvicorn.run("main:app", host="0.0.0.0", port=9000, workers=5)
从源代码输出代码
python3 main.py
Running the instance
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO: Started parent process [17828]
INFO: Started server process [17869]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [17870]
INFO: Waiting for application startup.
INFO: Application startup complete.
我使用 pyinstaller 使用以下命令创建一个文件
pyinstaller --onefile main.py
并在运行主文件时使用
./main
得到以下错误
Running the instance
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO: Started parent process [18964]
ERROR: Error loading ASGI app. Could not import module "main".
ERROR: Error loading ASGI app. Could not import module "main".
如何引用main:app,安装程序创建后实际的类名是什么?
我在某处读到我们需要使用像
foldername.main:app ,但这也不起作用