1

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.
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
  • Does this answer your question? [Signal handling in Uvicorn with FastAPI](https://stackoverflow.com/questions/71528875/signal-handling-in-uvicorn-with-fastapi) - there are other SO questions/answers that might apply to your case. Googling is always the best first course of action. – CryptoFool Oct 18 '22 at 19:30
  • That does not solve the issue. It is not related. I did Google the issue. I could find the answer. There is something related to `uvicorn`. – Sami Al-Subhi Oct 18 '22 at 20:09
  • So your problem is that after you get the output you show, your app doesn't quit, right? You don't say so explicitly. – CryptoFool Oct 18 '22 at 20:23
  • Anyways, It looks like something was not compatible between uvicorn, FastAPI and starlette around those versions. I updated them and that fixed it. – Sami Al-Subhi Oct 18 '22 at 20:29

2 Answers2

1

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.

Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
0

I've read about this problem in using uvicorn and I found the below code snippet to resolve that:

# 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

Reference:

CTRL^C doesn't work while startup in progress

Javad
  • 2,033
  • 3
  • 13
  • 23