我正在尝试将 FastAPI 作为 Windows 服务运行。找不到任何文档或任何文章来将 Uvicorn 作为 Windows 服务运行。
我也尝试使用 NSSM,但我的 Windows 服务停止了。
我设法使用 NSSM 将 FastAPI 与 uvicorn 作为 Windows 服务运行。
我不得不以编程方式部署 uvicorn https://www.uvicorn.org/deployment/#running-programmatically,基本上直接从 Python 脚本运行 uvicorn,然后使用 NSSM 创建并安装自定义服务。
这是一个基于 FastAPI 的小例子example https://fastapi.tiangolo.com/#example,但不是运行它uvicorn main:app --reload
从命令行,您添加uvicorn.run(app, **config)
用你自己的配置。
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info")
然后你可以使用标准的 NSSM 安装它nssm install
command https://nssm.cc/commands
nssm.exe install "FastAPIWindowsService" "C:\Scripts\FastAPIWindowsService\venv\Scripts\python.exe" "C:\Scripts\FastAPIWindowsService\src\main.py"
相应地更改您的服务名称、python.exe 的路径和脚本的路径。安装后,您的服务应该出现在 Windows 服务管理器中。
希望这对您有所帮助,并且对您有用!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)