question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uvicorn error on startup + reloader not working properly

See original GitHub issue

Problem description

I am running FastAPI inside docker, and the uvicorn reloader is not working as it should. I could not find an similar issue thus I am reporting it here to see if anybode ran into this. The API works fine, but without reloader development work involves shutting down and up docker, plus such an error is probably not fit for production. It still detects changes in the code but only after 1 or 2 minutes, but every time it reloads / starts up it throws the following warning (likely related):

api_1  | INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
api_1  | INFO:     Started reloader process [8] using statreload
api_1  | INFO:     Started server process [10]
api_1  | 2021-02-22 10:25:55 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/_impl/asyncio.py:62} | INFO :  Started server process [10]  
api_1  | INFO:     Waiting for application startup.
api_1  | 2021-02-22 10:25:55 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/lifespan/on.py:26} | INFO :  Waiting for application startup.
api_1  | INFO:     Application startup complete.
api_1  | 2021-02-22 10:25:55 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/lifespan/on.py:38} | INFO :  Application startup complete.  
...
api_1  | WARNING:  StatReload detected file change in 'app/utils/colorgram.py'. Reloading...
api_1  | INFO:     Shutting down
api_1  | 2021-02-22 10:30:22 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/_impl/asyncio.py:208} | INFO :  Shutting down
api_1  | INFO:     Waiting for application shutdown.
api_1  | 2021-02-22 10:30:22 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/lifespan/on.py:43} | INFO :  Waiting for application shutdown.
api_1  | INFO:     Application shutdown complete.
api_1  | 2021-02-22 10:30:22 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/lifespan/on.py:46} | INFO :  Application shutdown complete. 
api_1  | INFO:     Finished server process [10]
api_1  | 2021-02-22 10:30:22 | uvicorn.error | {/usr/local/lib/python3.7/site-packages/uvicorn/_impl/asyncio.py:75} | INFO :  Finished server process [10] 

My entrypoint.sh reads:

bash -c "uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"

Environment

  • OS: Docker (Linux)
  • FastAPI Version: 0.62.0
  • Python version: 3.7.9
  • Uvicorn: Running uvicorn 0.12.3 with CPython 3.7.9 on Linux

Additional context

Dockerfile

FROM python:3.7

# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV CODE_SRC=app

WORKDIR /code/
ENV PATH=$PATH:/code/
ENV PYTHONPATH=$PYTHONPATH:/code/

COPY pyproject.toml poetry.lock ./

RUN set -ex; \
    apt-get update ; \
    apt-get install ffmpeg libsm6 libxext6 -y ; \
    apt-get install jq -y ; \
    apt-get install -y --no-install-recommends git pkg-config ; \
    pip install poetry ; \
    poetry config virtualenvs.create false ; \
    poetry install --no-interaction --no-ansi ; \
    apt-get autoremove -y ; \
    apt-get clean -y ; \
    rm -rf /var/lib/apt/lists/*

COPY . .

ENTRYPOINT [ "./entrypoint.sh"]

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
illpackcommented, Feb 24, 2021

Hi there, I just solved it by explicitly passing uvicorn the path where my development code lives:

--reload-dir app

so the command becomes

bash -c "uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --reload-dir app"

Hope this helps!

0reactions
tiangolocommented, Nov 14, 2022

Thanks for the help here everyone! 👏 🙇

Thanks for reporting back and closing the issue @illpack 👍

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I’m checking each one in order.

Read more comments on GitHub >

github_iconTop Results From Across the Web

api - Problem enabling Uvicorn auto-restart when launching ...
The following statement is at the bottom of my api source code file and while Uvicorn starts and runs fine, it doesn't launch...
Read more >
Fixing Error for Uvicorn Python FastAPI - YouTube
WARNING: The -- reload flag should not be used in production on Windows. ERROR : Error loading ASGI app. Could not import module...
Read more >
Deployment - Uvicorn
Typically you'll run uvicorn from the command line. ... The ASGI application should be specified in the form path.to.module:instance.path . When running locally, ......
Read more >
tiangolo/fastapi - Gitter
Hello guys, trying to deploy a gunicorn/uvicorn/fastapi application, I'm a bit struggling to configure the logs. I initialize the root logger once with...
Read more >
error loading asgi app. could not import module app.main
$ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [40726] using statreload INFO: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found