Implementing background tasks
Background tasks are a useful feature that enables you to delegate resource-intensive operations to separate processes. With background tasks, your application can remain responsive and handle multiple requests simultaneously. They are particularly important for handling long-running processes without blocking the main request-response cycle. This improves the overall efficiency and scalability of your application. In this recipe, we will explore how you can execute background tasks in FastAPI applications.
Getting ready
To follow this recipe, all you need is a FastAPI application running with at least one endpoint to apply the background task. However, we will implement the background task into our trip platform into the GET /v2/trips/{category}
endpoint, defined in the Implementing dependency injection recipe.
How to do it…
Let’s imagine we want to store the message of the GET /v2/trips/{category}
endpoint in an external database...