首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FastAPI在按Ctr+c时不会退出

FastAPI在按Ctr+c时不会退出
EN

Stack Overflow用户
提问于 2022-10-18 19:24:27
回答 2查看 90关注 0票数 0

我发现退出FastAPI有困难。Ctr+c不起作用。这是我的pyproject.toml

代码语言:javascript
运行
复制
[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"

这是我的切入点

代码语言:javascript
运行
复制
"""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)

当我运行它,然后尝试退出时,进程会挂起,并且不会返回到终端:

代码语言:javascript
运行
复制
(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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-19 05:13:10

看起来,围绕这些版本,unvicorn、starlette和FastAPI之间存在着兼容性问题。我把它们更新到最新版本,从而解决了这个问题。

票数 0
EN

Stack Overflow用户

发布于 2022-10-18 19:40:40

我已经阅读了使用uvicorn时遇到的问题,并找到了下面的代码片段来解决这个问题:

代码语言:javascript
运行
复制
# 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

参考资料:

在启动过程中,CTRL^C不工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74116435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档