Background Tasks in FastAPI: A Comprehensive Guide

Background tasks in fast api

FastAPI, a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints, has a powerful feature called Background Tasks. These tasks are designed to handle time-consuming operations that you don’t want to block the main application flow. They allow your application to remain fast and responsive while handling these tasks in the background, providing a better user experience 4.

What are Background Tasks in FastAPI?

Background tasks in FastAPI are functions that run after being triggered by the main application. They are defined as functions that run after returning the response. This way, your client application is not waiting for the task to be completed. Some examples of tasks that can be handled in the background include submitting a job to perform data analytics, sending emails, processing large files, or performing complex calculations 9.

How to Use Background Tasks in FastAPI?

To use background tasks in FastAPI, you need to import BackgroundTasks from FastAPI and declare a parameter of type BackgroundTasks in your path operation function. You can then use the add_task method of the BackgroundTasks instance to add a task. Here is an example:

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

def process_data():

   # perform time-consuming task here

   pass

@app.post("/data/")

async def create_data(background_tasks: BackgroundTasks):

   # trigger background task

   background_tasks.add_task(process_data)

   return {"message": "Data created"}

In this example, process_data is the function that will run in the background and create_data is the main application route that triggers the background task using the background_tasks dependency, read more here .

Background Tasks and Dependency Injection

FastAPI’s background tasks also work with the dependency injection system. You can declare a parameter of type BackgroundTasks at multiple levels: in a path operation function, in a dependency (dependable), in a sub-dependency, etc. FastAPI knows what to do in each case and how to re-use the same object, so that all the background tasks are merged together and are run in the background afterwards.

Advanced Background Tasks with Celery

For more advanced and robust ways to perform tasks in the background in FastAPI, you can use Celery, which is a distributed task queue system. Celery allows you to define and execute tasks asynchronously, using workers that can run on different machines or processes. It also provides features such as retries, results, monitoring, scheduling, and more.

FastAPI Background Tasks examples

here are some examples of tasks that can be handled in the background using FastAPI’s Background Tasks:

  1. Sending Email Notifications: As connecting to an email server and sending an email tends to be slow (several seconds), you can return the response right away and send the email notification in the background. This way, the user doesn’t have to wait for the email to be sent before receiving the response.
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

def send_email(email: str):

   # code to send email

   pass

@app.post("/send-email/")

async def send_email_endpoint(background_tasks: BackgroundTasks, email: str):

   background_tasks.add_task(send_email, email)

   return {"message": "Email will be sent in the background"}
  1. Processing Large Files: If you receive a file that must go through a slow process, you can return a response of “Accepted” (HTTP 202) and process it in the background.
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

def process_large_file(file_path: str):

   # code to process large file

   pass

@app.post("/process-file/")

async def process_file_endpoint(background_tasks: BackgroundTasks, file_path: str):

   background_tasks.add_task(process_large_file, file_path)

   return {"message": "File will be processed in the background"}
  1. Sending Push Notifications: You can use FastAPI’s BackgroundTasks to send push notifications in the background. This can be useful if you have a service that needs to send notifications to users, but you don’t want to block the main application flow while waiting for the notifications to be sent.
import time

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

def send_push_notification(device_token: str):

   time.sleep(15) # simulates slow network call to firebase/sns

   with open("notification.log", mode="a") as notification_log:

       response = f"Successfully sent push notification to {device_token}\n"

       notification_log.write(response)

@app.get("/push/{device_token}")

async def notify(device_token: str, background_tasks : BackgroundTasks):

   background_tasks.add_task(send_push_notification, device_token)

   return {"message": "Notification sent"}
  1. Processing Data with Pandas: If you’re working with data and need to perform some heavy processing on it, you can use FastAPI’s BackgroundTasks to process the data in the background while the API continues to respond to other requests.
from fastapi import FastAPI, BackgroundTasks

import pandas as pd

app = FastAPI()

def process_data(data: list):

   df = pd.DataFrame(data)

   # perform some heavy processing on the dataframe

   pass

@app.post("/process-data/")

async def process_data_endpoint(background_tasks: BackgroundTasks, data: list):

   background_tasks.add_task(process_data, data)

   return {"message": "Data will be processed in the background"}

These are just a few examples of the types of tasks that can be handled in the background using FastAPI’s Background Tasks. The key is to identify any operation that is time-consuming and can be done after the response has been sent to the client.

How does FastAPI’s Background Tasks handle errors or exceptions during background tasks?

FastAPI’s Background Tasks handle errors and exceptions by running the tasks after the response is sent back to the client. This means that any exceptions that occur during the execution of the background task are not caught by FastAPI’s exception handlers and are not sent back to the client.

If an exception occurs during the execution of a background task, the exception will be raised and can be caught and handled in the same way as any other Python exception. However, because the background task is running after the response has been sent, the client will not be aware of the exception and will not receive an error response.

Here’s an example of how you can handle exceptions in a background task:

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

def process_data(data: list):

  try:

      # perform some heavy processing on the data

      pass

  except Exception as e:

      # handle the exception here

      print(f"An error occurred: {e}")

@app.post("/process-data/")

async def process_data_endpoint(background_tasks: BackgroundTasks, data: list):

  background_tasks.add_task(process_data, data)
return {"message": "Data will be processed in the background"}

If an exception occurs during the execution of the process_data function, it will be caught and handled within the function itself. Because the exception is handled within the function, the client will not be aware of the exception and will not receive an error response 6.

However, it’s important to note that FastAPI’s exception handlers will not catch exceptions that occur in background tasks. This means that if an exception occurs in a background task, it will not be logged by FastAPI’s exception handlers and will not be sent back to the client. Therefore, it’s important to handle exceptions within the background tasks themselves 10.

Conclusion

In conclusion, FastAPI background tasks are a powerful tool for running asynchronous tasks in the background of a FastAPI web application. They allow your application to remain fast and responsive while handling time-consuming tasks in the background, making for a better user experience. With its lightweight and efficient design, FastAPI is a great choice for building high-performance web applications that can handle complex processing tasks in the background 4.

SOURCES

Background Tasks – FastAPI – tiangolo

How to use BackgroundTasks in FastAPI | by Meta Heuristic – Medium

Background Tasks in FastAPI. In this article, we will explore the… | by …

FastAPI Background Tasks: Revolutionizing Asynchronous Programming in 

How to Run Background Tasks in FastAPI (2 Ways)

One thought on “Background Tasks in FastAPI: A Comprehensive Guide

Comments are closed.