InterviewHubPRO
Section Overview
Q46

Question 46 of 65

Part 4: Web Frameworks, APIs & FastAPI (41 - 55)

71%
Python & FastAPI•Part 4: Web Frameworks, APIs & FastAPI (41 - 55)

BackgroundTasks vs Celery:

In Simple Words:

  • FastAPI BackgroundTasks: Runs in-process in the same service after returning the HTTP response. Best for lightweight tasks (sending a welcome email).
  • Celery: Distributed task queue backed by Redis/RabbitMQ. Survives process crashes and handles heavy batch processing across external worker fleets.
python
from fastapi import BackgroundTasks

def send_email_notification(email: str):
    # Simulated email sending
    print(f"Sent email to {email}")

@app.post("/register")
async def register(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(send_email_notification, email)
    return {"message": "Registration successful, email queued"}