0
0
Flaskframework~8 mins

Task queue concept in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Task queue concept
HIGH IMPACT
Task queues affect how background tasks are handled, improving page responsiveness and reducing server blocking during user requests.
Handling long-running tasks in a Flask web app
Flask
from flask import Flask, request, jsonify
from celery import Celery

app = Flask(__name__)
celery = Celery(app.name, broker='redis://localhost:6379/0')

@celery.task
def long_task():
    import time
    time.sleep(10)
    return 'done'

@app.route('/process')
def process():
    task = long_task.delay()
    return jsonify({'task_id': task.id, 'status': 'processing'})
The long task runs asynchronously in a worker, freeing the web server to respond immediately.
📈 Performance GainNon-blocking request, reduces INP drastically, improves user experience.
Handling long-running tasks in a Flask web app
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/process')
def process():
    # Long task runs directly in request
    result = long_task()
    return f"Result: {result}"

def long_task():
    import time
    time.sleep(10)  # Simulates a slow task
    return 'done'
The long task blocks the request thread, causing slow response and poor user experience.
📉 Performance CostBlocks rendering and response for 10 seconds, causing high INP and poor LCP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous long task in requestMinimalN/ABlocks paint until task completes[X] Bad
Asynchronous task queue with CeleryMinimalN/AImmediate paint, task runs in background[OK] Good
Rendering Pipeline
Without a task queue, long tasks block the request thread, delaying response and browser rendering. With a task queue, the server quickly responds, allowing the browser to paint and interact sooner.
Request Handling
Response Time
Interaction to Next Paint (INP)
⚠️ BottleneckRequest Handling blocks due to synchronous long tasks.
Core Web Vital Affected
INP
Task queues affect how background tasks are handled, improving page responsiveness and reducing server blocking during user requests.
Optimization Tips
1Offload long-running tasks to background workers to keep the server responsive.
2Avoid blocking request threads with synchronous heavy processing.
3Use task queues like Celery with Redis or RabbitMQ for scalable background jobs.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using a task queue in a Flask app for long-running tasks?
AIt improves CSS selector performance.
BIt prevents the web server from blocking during requests.
CIt reduces the size of the Flask app bundle.
DIt decreases the number of DOM nodes.
DevTools: Performance
How to check: Record a performance profile while triggering the long task endpoint; observe main thread blocking time and responsiveness.
What to look for: Look for long tasks blocking the main thread and delayed first input response indicating poor INP.