The --reload flag should not be used in production on Windows. Uvicorn server warning

Viewed 2023

I run uvicorn main:app --reload to start a FastAPI server. the server is running, but I see a warning at the console that is:

WARNING:  The --reload flag should not be used in production on Windows.

Below is my code:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()


origins = ["https://localhost:8080"]

app.add_middleware(
  CORSMiddleware,
  allow_origins=origins,
  allow_credentials=True,
  allow_methods=["*"],
  allow_headers=["*"])

@app.get("/")
def create_todo():
  return {"Ping":"Pong"}

I would like to know that Why am I seeing that? What is the reason for that warning?

2 Answers

As per FastAPI documentation:

Warning

Remember to remove the --reload option if you were using it.

The --reload option consumes much more resources, is more unstable, etc.

It helps a lot during development, but you shouldn't use it in production.

Thus, FastAPI displays that warning as a reminder for you not to use the --reload flag in production, as the server would consume much more resources to periodically look for changes to all *.py files.

It is, however, perfectly fine to use the --reload flag as you develop and test your application on your own machine. You can also specify which directories to watch for python file changes, as well as specify a glob pattern to match files or directories which will included or excluded from watching - see Uvicorn documentation.

There are multiple "stages" that we usually talk about in how an application is used (development, testing, staging, production, etc.); in this case, only development and production is relevant.

development refers to you (the developer) running the application on your own computer and actively developing the application. In this situation using --reload is perfectly fine - it's the usage it is intended for! It's also the use case when it's actually useful, since the code changes as you develop your application and write code.

production refers to the stage where your application is made available to other people, usually in a secondary location - on a server or some other service - where the code doesn't actively change any longer (just after you've made your changes and decided that it's time to update the application version that other people see and use).

When you deploy your application to production the code doesn't actively change while the application is running - you develop on your own computer, but on the server code doesn't change before you upload or deploy it to the server. Changing the code at that stage is a more deliberate decision, and when that happens, you restart the application manually after you've deployed the new code. In that case running with the --reload flag just add unnecessary overhead, since the server has to watch all the files in the application for changes - changes that never happen.

This is particularly the case on Windows if the number of files are high, and therefor the message mentions Windows explicitly. I'd skip using it on other platforms as well, but the performance hit isn't as large there.

Related