Performance: Background tasks
MEDIUM IMPACT
Background tasks affect server response time and user interaction speed by offloading work from the main request cycle.
from fastapi import FastAPI, BackgroundTasks app = FastAPI() def send_email_sync(email: str, content: str): # Slow operation pass @app.post('/send-email') async def send_email(data: dict, background_tasks: BackgroundTasks): background_tasks.add_task(send_email_sync, data['email'], data['content']) return {'status': 'email scheduled'}
from fastapi import FastAPI app = FastAPI() @app.post('/send-email') async def send_email(data: dict): # Slow operation done inline send_email_sync(data['email'], data['content']) return {'status': 'email sent'}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous slow task in request | N/A (server-side) | N/A | N/A | [X] Bad |
| Background task after response | N/A (server-side) | N/A | N/A | [OK] Good |