How do you run a FastAPI app?

FastAPI applications are designed to run on an ASGI (Asynchronous Server Gateway Interface) server. This compatibility ensures that FastAPI can operate seamlessly across various modern web servers.

Development Environment

For development, Uvicorn is the standard choice. Assuming your main file is named main.py and your FastAPI instance is named app, you can start the server using the following command:

uvicorn main:app --reload

The --reload flag is particularly useful during development, as it automatically restarts the server whenever code changes are detected.

Production Environment

In a production setting, performance and stability are critical. It is common practice to use Gunicorn acting as a process manager in combination with Uvicorn workers. This setup allows for better handling of concurrent requests and process management.

The ASGI Standard

Because FastAPI adheres to the ASGI standard, it provides high-performance asynchronous capabilities. This makes it significantly faster than traditional WSGI-based frameworks when handling long-lived connections and asynchronous tasks.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents