Performance: Celery integration overview
This affects backend task processing speed and responsiveness of the Flask web app by offloading long-running tasks.
from flask import Flask from celery import Celery app = Flask(__name__) celery = Celery(app.name, broker='redis://localhost:6379/0') @celery.task def long_task(): # Long task runs asynchronously return 'done' @app.route('/process') def process(): task = long_task.delay() return f"Task started: {task.id}"
from flask import Flask app = Flask(__name__) @app.route('/process') def process(): # Long task runs synchronously result = long_task() return f"Result: {result}"
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous long task in Flask route | Minimal | 0 | 0 | [X] Bad |
| Asynchronous task with Celery | Minimal | 0 | 0 | [OK] Good |
@celery_app.task decorator.@celery_app.task
def multiply(x, y):
return x * y
result = multiply.delay(3, 4)
print(result.get())
What will be printed?multiply.delay(3, 4) sends the task to Celery to run asynchronously.result.get() waits for the task to finish and returns the output, which is 3 * 4 = 12.@celery_app.task def add(x, y): return x + yBut when you call
add.delay(2, 3), it raises an IndentationError. What is the problem?return x + y is not indented, causing IndentationError.@celery_app.task, and are called with delay().Pass the test and this pattern moves unknown