import uvicorn
from fastapi import FastAPI
app = FastAPI(title='MADS API')
uvicorn.run(app, host='0.0.0.0', port=8127, workers=2)
WARNING: You must pass the application as an import string to enable 'reload' or 'workers'.
than it quits with SystemError 1
Corresponding ticket in uvicorn
Versions:
$ pip3 list |grep "uvicorn\|fastapi"
fastapi 0.55.1
uvicorn 0.11.5
Why do you say it is a bug?
It is your first try at FastAPI?
No, i upgraded from Ubuntu 18.04 to Ubuntu 20.04. It was running fine on Ubuntu 18.04. productive a long time like this. So it looks like the interfaces were changing?
You must pass the application as an import string to enable 'reload' or 'workers'.
Is the program stopping here? Looks like it's only a warning but it's very straightforward about what's going on.
@phy25 Yes it stops here. I was searching through a bit, and found this issue report on uvicorn under Windows.
When I try it as supposed in the related issues, it gives me errors as well:
uvicorn.run("app", host='0.0.0.0', port=8127, workers=2)
ERROR: Error loading ASGI app. Import string "app" must be in format "<module>:<attribute>".
and
uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR: Error loading ASGI app. Could not import module "main".
So uvicorn did not found the FastAPI application.
This is intended and not related to fastapi:
https://github.com/encode/uvicorn/blob/9b92925a352b9743c5cfcef4a65e74a81a1bad4f/uvicorn/main.py#L343
You need to run it with command line uvicorn if you want to run with multiple workers. Please read uvicorn documentation.
Thanks for the help here @phy25 ! :clap: :bow:
If that solves the original problem, then you can close this issue @cgi1 :heavy_check_mark:
The answer I was searching for is here: You can actually start it the way I wanted it, but make sure to name it correctly:
uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR: Error loading ASGI app. Could not import module "main".
raises Error because the file is not named main.py! If you have it as my_fastapi_server.py, you run it as follows:
uvicorn.run("my_fastapi_server:app", host='0.0.0.0', port=8127, workers=2)
Adding another snippet as the ones posted here were pretty cryptic for me.
With the following files:
app\main.py # Containing an app object
debug_server.py
I needed my debug_server.py to be:
import uvicorn
# Importing app here makes the syntax cleaner as it will be picked up by refactors
from app.main import app
if __name__ == "__main__":
uvicorn.run("debug_server:app", host="0.0.0.0", port=80, reload=True)
That way the debug auto-reload server did work properly and there鈥檚 nothing related to it in the /app folder.
Most helpful comment
Adding another snippet as the ones posted here were pretty cryptic for me.
With the following files:
I needed my
debug_server.pyto be:That way the debug auto-reload server did work properly and there鈥檚 nothing related to it in the
/appfolder.