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

Time:03-07

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

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

For Fast API my code is

    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 want to know that Why I am seeing that... what is the reason to see this warning...

CodePudding user response:

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 to you not to use --reload in production.

CodePudding user response:

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