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.

Run uvicorn programmatically with `reload=True` not in if-name-main block print irrelated error message

See original GitHub issue

Checklist

  • 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
euri10commented, May 27, 2021

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)

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

so indeed you have to use the __main__ block if you want to run uvicorn However I’m unable to reproduce your Address already in use message, probably a difference in os, can someone on a mac see if he can ?

0reactions
euri10commented, May 28, 2021

I think this issue is likely caused by same reason with #949. IMO, write something in doc to point the proper idiom in the main module is required while using some options like ‘debug/reload’ is not a bad thing anyway.

ok let’s close those 2 issues by adding a warning to the docs, fancy sending a PR @TLDay ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deployment - Uvicorn
Run uvicorn --reload from the command line for local development. Run gunicorn -k uvicorn.workers.UvicornWorker for production. Additionally run behind Nginx ...
Read more >
Python - How to use FastAPI and uvicorn.run without blocking ...
My problem is that I want to start the Server from another process that should go on with other tasks after starting the...
Read more >
Run a Server Manually - Uvicorn - FastAPI
The main thing you need to run a FastAPI application in a remote server machine is ... Uvicorn, a lightning-fast ASGI server, built...
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