0
0
Djangoframework~8 mins

Task results and status in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Task results and status
MEDIUM IMPACT
This concept affects how quickly users see task completion feedback and how efficiently the server handles background task updates.
Updating task status on the frontend after a background task completes
Django
def start_task_view(request):
    task = background_task.delay()
    return JsonResponse({'task_id': task.id, 'status': 'started'})

# Frontend polls or uses WebSocket to get status updates asynchronously
Starts task asynchronously and immediately responds, allowing UI to stay responsive while polling for updates.
📈 Performance GainNon-blocking response, reduces input delay, improves INP metric.
Updating task status on the frontend after a background task completes
Django
def task_view(request):
    result = long_running_task()
    return JsonResponse({'status': 'done', 'result': result})
This blocks the request until the task finishes, delaying response and freezing the UI.
📉 Performance CostBlocks rendering for the entire task duration, causing high INP and poor user experience.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous task execution in requestMinimal DOM changes0 reflows during task but blocks responseDelayed paint due to blocking[X] Bad
Asynchronous task with polling or WebSocketIncremental DOM updates on status change1 reflow per updateSmooth paint with small incremental changes[OK] Good
Rendering Pipeline
Task status updates flow from server to client asynchronously, avoiding blocking the main thread and allowing smooth UI updates.
Network
JavaScript Execution
Paint
Composite
⚠️ BottleneckBlocking synchronous task execution delays network response and JavaScript updates.
Core Web Vital Affected
INP
This concept affects how quickly users see task completion feedback and how efficiently the server handles background task updates.
Optimization Tips
1Never block HTTP responses with long-running tasks.
2Use asynchronous task queues like Celery for background processing.
3Update task status asynchronously via polling or WebSocket to keep UI responsive.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with running a long task synchronously in a Django view?
AIt blocks the response, causing high input delay and poor user experience.
BIt increases the bundle size of the frontend assets.
CIt causes excessive CSS recalculations.
DIt triggers multiple layout shifts on the page.
DevTools: Performance
How to check: Record a session while triggering a task and observe the Main thread activity and network requests for blocking or asynchronous behavior.
What to look for: Look for long tasks blocking the main thread and delayed network responses indicating synchronous blocking.