Performance: Defining Celery tasks
MEDIUM IMPACT
This affects the responsiveness of the Flask web app by offloading long-running tasks to background workers, improving user interaction speed.
from flask import Flask from celery import Celery app = Flask(__name__) celery = Celery('tasks', broker='redis://localhost:6379/0') @celery.task def long_running_task(): # Long task code here return 'done' @app.route('/process') def process(): task = long_running_task.delay() return f"Task started: {task.id}"
from flask import Flask app = Flask(__name__) @app.route('/process') def process(): # Long task runs here result = long_running_task() return f"Result: {result}"
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous task in Flask route | Minimal | 0 | 0 | [X] Bad |
| Asynchronous Celery task with immediate response | Minimal | 0 | 0 | [OK] Good |