How to gracefully stop FastAPI app?
  • 24-May-2023
Lightrun Team
Author Lightrun Team
Share
How to gracefully stop FastAPI app?

How to gracefully stop FastAPI app ?

Lightrun Team
Lightrun Team
24-May-2023

Explanation of the problem

 

The issue at hand pertains to achieving a graceful stop of a FastAPI application. The concept of a “graceful stop” is of utmost importance in modern web applications as it enables the prevention of data loss or interruption of in-flight requests during application restarts or shutdowns, particularly in scenarios involving rolling updates. While frameworks like Go and Spring Boot offer built-in solutions for graceful stop functionality, the specific approach for achieving this in a FastAPI application has proven elusive. Despite an extensive search through the FastAPI forum and general online resources, comprehensive information on implementing graceful stop in a FastAPI application remains scarce. Notably, the official “uvicorn-gunicorn-fastapi-docker” repository mentions a “graceful_timeout” parameter within the “gunicorn” server configuration, but the uncertainty arises when using the “uvicorn” command to start FastAPI within a custom Docker environment, instead of relying on “gunicorn”.

 

Troubleshooting with the Lightrun Developer Observability Platform

 

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: How to gracefully stop FastAPI app ?

 

To achieve a graceful stop in a FastAPI application when using the “uvicorn” command instead of “gunicorn”, one possible solution is to implement a custom signal handler. This approach involves capturing the termination signal (SIGTERM) and performing the necessary cleanup tasks before shutting down the application.

Here’s an example of how you can implement the signal handler in your FastAPI application code:

 

import signal
import sys
from fastapi import FastAPI

app = FastAPI()

# Define the signal handler function
def graceful_shutdown(signum, frame):
    # Perform cleanup tasks here (e.g., closing database connections, saving state, etc.)
    # ...

    # Exit the application
    sys.exit(0)

# Register the signal handler for SIGTERM
signal.signal(signal.SIGTERM, graceful_shutdown)

# Your FastAPI routes and logic here
# ...

if __name__ == "__main__":
    # Start the FastAPI application using uvicorn
    uvicorn.run("app:app", host='0.0.0.0', port=8000, reload=True, debug=True, workers=3)

 

In the code snippet above, we import the signal and sys modules to handle the termination signal and exit the application gracefully. The graceful_shutdown function is defined to encapsulate the necessary cleanup tasks. You can customize this function to include any specific actions you need to perform before shutting down the application. Finally, we register the signal handler for the SIGTERM signal using signal.signal(signal.SIGTERM, graceful_shutdown). This ensures that when the termination signal is received, the graceful_shutdown function will be executed.

By implementing this custom signal handler, you can gracefully stop your FastAPI application when using the “uvicorn” command, ensuring that any pending requests are completed and critical data is not lost during the shutdown process.

 

Problems with fastapi

 

Problem 1: CORS (Cross-Origin Resource Sharing) Issues

One common problem encountered with FastAPI is dealing with CORS issues. When making cross-origin requests from a web browser, the browser enforces security policies that restrict access to resources on different domains. FastAPI applications hosted on one domain may encounter CORS errors when attempting to make requests to another domain.

To solve this issue, FastAPI provides a middleware called fastapi.middleware.cors. By adding this middleware to your application, you can configure the necessary CORS headers to allow cross-origin requests. Here’s an example of how to use the CORS middleware in FastAPI:

 

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

app = FastAPI()

# Configure CORS settings
origins = [
    "http://localhost",
    "http://localhost:3000",
]

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

 

In the code snippet above, we create a list of allowed origins that are allowed to make cross-origin requests. We then add the CORSMiddleware to the FastAPI application using app.add_middleware(), passing in the necessary configuration options. This ensures that the necessary CORS headers are included in the server responses, allowing cross-origin requests to be made successfully.

Problem 2: Request Validation and Serialization

Another common problem in FastAPI is request validation and serialization. FastAPI provides powerful request validation capabilities out of the box, allowing you to define request models and automatically validate and deserialize incoming requests. However, when dealing with complex data structures or nested models, ensuring proper validation and serialization can become challenging.

To address this issue, you can leverage Pydantic, which is integrated with FastAPI and provides robust data validation and serialization features. By creating Pydantic models for your request bodies, you can define the expected structure, types, and constraints. Here’s an example:

 

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: Item):
    # Access validated and deserialized item properties
    item_name = item.name
    item_price = item.price
    # ...

 

In the code snippet above, we define a Item Pydantic model that represents the structure of the request body. By specifying the expected types and constraints, FastAPI will automatically validate and deserialize the incoming request. This ensures that only valid data is processed in your API endpoints, reducing the risk of errors and improving the reliability of your application.

Problem 3: Authentication and Authorization

Implementing authentication and authorization mechanisms is a common challenge in web applications, and FastAPI provides various options to address this problem. However, it can still be complex to implement and manage authentication and authorization in a secure and scalable manner.

One solution is to leverage third-party libraries or frameworks for authentication and authorization, such as OAuth 2.0 or JWT (JSON Web Tokens). These libraries provide standardized protocols and mechanisms for authentication and authorization, reducing the complexity of implementation. Here’s an example using OAuth2 and FastAPI OAuth2 support:

 

from fastapi import FastAPI
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token")

@app.get("/items/")
async def get_items(token: str = Depends(oauth2_scheme)):
    # Validate and verify the token
    # Perform authorization checks
    # ...

 

In the code snippet above, we define an OAuth2 password bearer scheme using the OAuth2PasswordBearer class. This enables authentication and authorization through the token parameter in the get_items endpoint. By specifying the Depends(oauth2_scheme) dependency, FastAPI will handle token validation and verification automatically.

By leveraging these authentication and authorization libraries and integrating them with FastAPI, you can ensure that your application’s endpoints are secure and accessible only to authenticated and authorized users.

 

A brief introduction to fastapi

 

FastAPI is a modern, fast, and highly efficient web framework for building APIs with Python. It is designed to provide a high-performance alternative to traditional web frameworks by leveraging asynchronous programming and type annotations. Built on top of Starlette and Pydantic, FastAPI combines the best features of these libraries to deliver a seamless development experience.

One of the key advantages of FastAPI is its performance. It utilizes asynchronous programming techniques, such as async and await, to handle multiple concurrent requests efficiently. This allows FastAPI to handle high loads and deliver excellent performance, making it well-suited for applications that require fast response times and scalability. Additionally, FastAPI utilizes type annotations to automatically generate interactive API documentation. With the help of tools like Swagger UI and ReDoc, developers can easily explore and interact with the API endpoints, making it easier to understand and test the API.

FastAPI also provides strong validation and serialization capabilities through its integration with Pydantic. By defining models using Pydantic’s expressive syntax, developers can specify the expected structure, types, and constraints of the request and response data. FastAPI leverages these models to automatically validate and deserialize incoming requests, reducing the likelihood of data-related errors. Furthermore, FastAPI provides built-in support for various authentication and authorization mechanisms, enabling developers to secure their APIs with ease. Overall, FastAPI’s combination of performance, documentation generation, validation, and security features make it a powerful choice for building high-quality web APIs.

 

Most popular use cases for fastapi

 

  1. Building High-Performance APIs: FastAPI excels at building high-performance APIs due to its asynchronous nature and efficient request handling. With the help of async and await keywords, developers can write non-blocking code that can handle multiple concurrent requests. Here’s an example of defining an asynchronous route handler in FastAPI:

 

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    # Perform asynchronous operations here
    # ...
    return {"item_id": item_id}

 

  1. Interactive API Documentation: FastAPI provides automatic interactive API documentation generation, making it easier for developers and users to explore and understand the available endpoints, request/response models, and input validation rules. FastAPI leverages the type annotations in Python to generate the documentation, which can be accessed through the Swagger UI or ReDoc. Here’s an example of defining a route with request and response models:

 

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: Item):
    # Process the item data here
    # ...
    return {"message": "Item created"}

 

  1. Integration with Python Ecosystem: FastAPI seamlessly integrates with various Python libraries and frameworks, allowing developers to leverage the existing ecosystem. It works well with ORMs like SQLAlchemy and databases such as PostgreSQL and MongoDB. FastAPI also supports authentication and authorization mechanisms, including OAuth2 and JWT, making it easy to secure API endpoints. Additionally, FastAPI supports dependency injection, allowing for clean and modular code organization. This integration with the Python ecosystem enables developers to build robust and scalable applications.

 

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.