Search code examples
pythonfastapiuvicornasgi

FastAPI is not quitting when pressing Ctr+c


I am finding a difficulty with quitting FastAPI. Ctr+c does not work. Here is my 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"

here is my entry point

"""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)

This what I get when I run it and then try to quit, the process hangs and does not go back to terminal:

(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.

Solution

  • It looks like there was a compatibility issue between unvicorn, starlette and FastAPI around those versions. I updated them to the latest versions and that solved the issue.