Run uvicorn programmatically with `reload=True` not in if-name-main block print irrelated error message
See original GitHub issueChecklist
- The bug is reproducible against the latest release and/or
master
. - There are no similar issues or pull requests to fix it yet.
Describe the bug
When you run uvicorn programmatically, if __name__ == '__main__'
block is required if you put reload=True
as parameter to uvicorn.run
function. Or you will got
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [$PID] using watchgod
ERROR: [Errno 48] Address already in use
I checked all tcp ports in LISTEN status, 8000 port is free to use and all processes I can recognize them.(They are not zombie watchgod process or something like that).
Using command like uvicorn --reload app:app
works perfectly in same project.
Put entry code into if-name-main block solved the problem.
Sorry of my poor English, I’m not a native speaker.😅
To reproduce
- app.py
import fastapi
app = fastapi.FastAPI()
- dev.py
import uvicorn
uvicorn.run('app:app', reload=True)
run python dev.py
will print error message said Address already in use
.
Expected behavior
Best: Put entry code into if-name-main block is not required.
Good: Print error message or other information to indicate the real problem.
Write warning or note in doc.
Actual behavior
Print irrelated error message Address already in use
.
Debugging material
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [$PID] using watchgod
ERROR: [Errno 48] Address already in use
Environment
- OS / Python / Uvicorn version: MacOS 11.3.1 / Python 3.9.5 / uvicorn 0.13.4
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
if I do
python dev.py
I have this error which is quite explicit about multiprocessing, since we dont use fork but spawn (see https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods)so indeed you have to use the
__main__
block if you want to run uvicorn However I’m unable to reproduce yourAddress already in use
message, probably a difference in os, can someone on a mac see if he can ?ok let’s close those 2 issues by adding a warning to the docs, fancy sending a PR @TLDay ?