FastAPI, uvicorn.run() always create 3 instances, but I want it 1 instance

2 min read 05-10-2024
FastAPI, uvicorn.run() always create 3 instances, but I want it 1 instance


Running FastAPI with Uvicorn: One Instance, Not Three

When you're building a FastAPI application and using Uvicorn to run it, you might notice that uvicorn.run() seems to spawn three instances of your application, creating confusion and potentially impacting resource usage. This article will explain why this happens, how to ensure you only run a single instance, and provide insights into managing your FastAPI application with Uvicorn.

Understanding the Problem:

The apparent creation of three instances stems from the way Uvicorn handles multiprocessing for performance optimization. By default, it uses the uvicorn.workers.UvicornWorker class, which leverages multiprocessing to handle incoming requests across multiple worker processes. These workers operate in parallel, aiming to improve your application's speed and throughput.

Code Example:

from fastapi import FastAPI
import uvicorn

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World!"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

In this simple example, when you run python main.py (assuming 'main.py' is the name of your file), you might see three Uvicorn processes running. While this is normal behavior for Uvicorn, it might not be desirable in all scenarios.

Running a Single Instance:

To run your FastAPI application with a single instance, you need to disable the multiprocessing behavior of Uvicorn. You can achieve this by setting the workers argument to 1 in the uvicorn.run() call.

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000, workers=1)

This modification ensures that only one worker process is spawned, running your FastAPI application as a single instance.

Additional Considerations:

  • Performance Trade-Off: While a single instance might be ideal for resource management and simpler configurations, it might affect your application's performance. If you need to handle high traffic volumes, it's recommended to explore Uvicorn's multiprocessing options and experiment with different worker counts to find the optimal balance between performance and resource usage.
  • Process Management: Tools like gunicorn or uvicorn's built-in reload option offer advanced process management features, including automatic restarts, graceful shutdowns, and worker monitoring. These tools can be beneficial for production environments, enabling smoother operation and better control over your application's lifecycle.

Conclusion:

Understanding the intricacies of Uvicorn's multiprocessing behavior is key to running your FastAPI application effectively. While the default behavior utilizes multiple workers for improved performance, you can easily configure it to run with a single instance by setting workers=1 in uvicorn.run(). Choose the configuration that best aligns with your application's needs and performance requirements.

Resources: