Monday 25 October 2021

FastAPI: uvicorn: make the server restart after code changes

--reload option is used to restart the server after every code change.

 

Let’s try it with an example.

 

main.py

from fastapi import FastAPI

app = FastAPI()

# Create an endpoint
@app.get("/")
def about_me():
    return {"name" : "Hello World app", "version": "1.0.0"}

Open terminal and execute the command ‘uvicorn main:app --reload'

 

$uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [56954] using statreload
INFO:     Started server process [56956]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Open the url ‘http://127.0.0.1:8000’ in browser.


 


 

Now update main.py file by changing the version number from 1.0.0 to 2.0.0. You will see below messages in console.

 

WARNING:  StatReload detected file change in 'main.py'. Reloading...
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [57395]
INFO:     Started server process [57661]
INFO:     Waiting for application startup.
INFO:     Application startup complete.


Reload the home page in browser, you can see that new version reflected.


Note:

Use –reload option only for development.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment