In FastAPI, background tasks are implemented using the BackgroundTasks
class. When a request is received, you can add functions to be executed as background tasks. These functions will run in the background after the response has been sent to the client. This separation ensures that the client gets a quick response, and the time - consuming tasks do not block the main request - handling flow.
The BackgroundTasks
class is a dependency that can be injected into your route handlers. You simply add the function you want to run in the background to the BackgroundTasks
instance, and FastAPI will take care of the rest.
from fastapi import FastAPI, BackgroundTasks
import time
app = FastAPI()
def write_log(message: str):
time.sleep(2) # Simulate a time - consuming task
with open("log.txt", "a") as f:
f.write(message + "\n")
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
message = f"Notification sent to {email}"
background_tasks.add_task(write_log, message)
return {"message": "Notification sent in the background"}
In this example, when a client makes a POST request to /send - notification/{email}
, the send_notification
route handler will immediately return a response to the client. Meanwhile, the write_log
function will be executed in the background, writing the message to the log.txt
file after a 2 - second delay.
As shown in the previous example, logging is a common use case for background tasks. You can log important events, user actions, or system information without affecting the response time for the client.
Sending emails can be a time - consuming process, especially if you are using a third - party email service. By using background tasks, you can send emails in the background while quickly returning a response to the client.
import smtplib
from email.mime.text import MIMEText
def send_email(to_email: str, subject: str, body: str):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "your_email@example.com"
msg['To'] = to_email
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login("your_email@example.com", "your_password")
server.sendmail("your_email@example.com", to_email, msg.as_string())
server.quit()
@app.post("/email/{to_email}")
async def send_email_route(to_email: str, background_tasks: BackgroundTasks):
subject = "Test Email"
body = "This is a test email sent from FastAPI."
background_tasks.add_task(send_email, to_email, subject, body)
return {"message": "Email sent in the background"}
If you have data that needs to be processed, such as cleaning, aggregating, or transforming, you can use background tasks to perform these operations. For example, you can process user - uploaded files in the background.
When using background tasks, it’s important to handle errors properly. Since the background tasks run independently of the main request - handling flow, errors in background tasks may not be immediately visible. You can add logging or monitoring to the background task functions to catch and handle errors.
def error_handling_task():
try:
# Some code that may raise an error
result = 1 / 0
except Exception as e:
with open("error_log.txt", "a") as f:
f.write(f"Error in background task: {str(e)}\n")
Make sure to release any resources used by the background tasks. For example, if you open a file or a database connection in a background task, close them properly to avoid resource leaks.
def resource_management_task():
file = open("test.txt", "w")
try:
file.write("Some data")
finally:
file.close()
If you have a large number of background tasks, you may want to limit the concurrency to avoid overloading the system. You can use a task queue or a thread pool to manage the execution of background tasks.
Background tasks in FastAPI are a powerful feature that allows you to improve the performance and responsiveness of your applications. By offloading time - consuming tasks to the background, you can ensure that your clients receive quick responses. We have covered the fundamental concepts, usage methods, common practices, and best practices of using background tasks in FastAPI. By following these guidelines, you can use background tasks efficiently and build more robust and scalable applications.