In FastAPI, background tasks are operations that are executed after a response has been sent to the client. These tasks run in the background, allowing the main application to continue processing other requests without waiting for the background operation to complete. This is particularly useful for tasks that are time - consuming and do not need to be completed immediately for the client to receive a response.
FastAPI uses the BackgroundTasks
class from the fastapi
module. When a request is received, you can add tasks to the BackgroundTasks
instance. Once the response is ready to be sent back to the client, FastAPI will start executing these background tasks.
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
def write_notification(email: str, message=""):
with open("log.txt", mode="w") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_notification, email, message="Some notification")
return {"message": "Notification sent in the background"}
In this example, when a client makes a POST request to /send - notification/{email}
, the main application will immediately return a response indicating that the notification is being sent in the background. Meanwhile, the write_notification
function will be executed asynchronously in the background.
Sending emails is a common use case for background tasks. Since sending an email can be a slow process, especially if there are network issues or if the email server is busy, it is a good idea to offload this task to the background.
import smtplib
from email.mime.text import MIMEText
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
def send_email(email: str, subject: str, body: str):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = '[email protected]'
msg['To'] = email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login('[email protected]', 'your_password')
server.sendmail('[email protected]', email, msg.as_string())
@app.post("/send-email/{email}")
async def send_email_endpoint(email: str, background_tasks: BackgroundTasks):
subject = "Test Email"
body = "This is a test email sent in the background."
background_tasks.add_task(send_email, email, subject, body)
return {"message": "Email sent in the background"}
Generating reports, such as PDF reports or Excel spreadsheets, can be a time - consuming process. By using background tasks, you can allow the user to receive an immediate response while the report is being generated in the background.
import pandas as pd
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
def generate_report():
data = {'Name': ['John', 'Jane', 'Doe'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df.to_excel('report.xlsx', index=False)
@app.get("/generate-report")
async def generate_report_endpoint(background_tasks: BackgroundTasks):
background_tasks.add_task(generate_report)
return {"message": "Report generation started in the background"}
When using background tasks, it is important to handle errors properly. Since these tasks run asynchronously, errors may not be immediately visible. You can add logging statements to the background task functions to record any errors that occur.
import logging
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
logging.basicConfig(level=logging.ERROR)
def task_with_error():
try:
# Some code that may raise an error
result = 1 / 0
except Exception as e:
logging.error(f"Error in background task: {e}")
@app.get("/run-task")
async def run_task(background_tasks: BackgroundTasks):
background_tasks.add_task(task_with_error)
return {"message": "Task started in the background"}
Make sure to release any resources used by the background tasks properly. For example, if you open a file or a database connection in a background task, close them after the task is completed.
Monitor the performance of your background tasks. If you notice that certain tasks are taking too long or consuming too many resources, consider scaling your application or optimizing the task code.
FastAPI’s background tasks are a powerful feature that can significantly improve the performance and responsiveness of your web applications. By offloading time - consuming operations to the background, you can ensure that your application can handle more requests and provide a better user experience. However, it is important to follow best practices such as error handling, resource management, and monitoring to make the most of this feature.