我发现退出FastAPI有困难。Ctr+c
不起作用。这是我的pyproject.toml
[tool.pyright]
exclude = ["app/worker"]
ignore = ["app/worker"]
[tool.poetry]
name = "api"
version = "0.1.0"
description = ""
authors = ["SamiAlsubhi <sami@alsubhi.me>"]
[tool.poetry.dependencies]
python = ">=3.8,<3.9"
fastapi = "^0.65.2"
tortoise-orm = "^0.17.4"
asyncpg = "^0.23.0"
aerich = "^0.5.3"
networkx = "^2.5.1"
numpy = "^1.21.0"
ldap3 = "^2.9.1"
fastapi-jwt-auth = "^0.5.0"
python-multipart = "^0.0.5"
torch = "1.7.1"
pyts = "0.11.0"
Pint = "^0.17"
Cython = "^0.29.24"
python-dotenv = "^0.19.0"
arq = "^0.22"
uvicorn = {extras = ["standard"], version = "^0.15.0"}
[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
requests = "^2.25.1"
asynctest = "^0.13.0"
coverage = "^5.5"
pytest-html = "^3.1.1"
pytest-sugar = "^0.9.4"
pytest-json-report = "^1.4.0"
pytest-cov = "^2.12.1"
pylint = "^2.11.1"
autopep8 = "^1.5.7"
black = "^22.3.0"
aiosqlite = "^0.17.0"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
这是我的切入点
"""running API in a local dev environment"""
import os
import uvicorn
from dotenv import load_dotenv
# laoding env values
load_dotenv("../.env")
if __name__ == "__main__":
port = os.getenv("FASTAPI_PORT")
port = int(port) if port else None
uvicorn.run("app.main:app", host=os.getenv("FASTAPI_HOST"),
port=port, reload=True)
当我运行它,然后尝试退出时,进程会挂起,并且不会返回到终端:
(trendr) sami@Samis-MBP backend % python run.py
INFO: Will watch for changes in these directories: ['/Users/name/Desktop/etc']
INFO: Uvicorn running on http://0.0.0.0:1000 (Press CTRL+C to quit)
INFO: Started reloader process [70087] using watchgod
INFO: Started server process [70089]
INFO: Waiting for application startup.
INFO: Application startup complete.
^CINFO: Shutting down
INFO: Finished server process [70089]
INFO: ASGI 'lifespan' protocol appears unsupported.
发布于 2022-10-19 05:13:10
看起来,围绕这些版本,unvicorn、starlette和FastAPI之间存在着兼容性问题。我把它们更新到最新版本,从而解决了这个问题。
发布于 2022-10-18 19:40:40
我已经阅读了使用uvicorn
时遇到的问题,并找到了下面的代码片段来解决这个问题:
# Add the below code snippet to your app.py module after the app initialization.
def receive_signal(signalNumber, frame):
print('Received:', signalNumber)
sys.exit()
@app.on_event("startup")
async def startup_event():
import signal
signal.signal(signal.SIGINT, receive_signal)
# startup tasks
参考资料:
https://stackoverflow.com/questions/74116435
复制相似问题