Performance: Task results and status
This concept affects how quickly users see task completion feedback and how efficiently the server handles background task updates.
Jump into concepts and practice - no test required
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
def task_view(request): result = long_running_task() return JsonResponse({'status': 'done', 'result': result})
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous task execution in request | Minimal DOM changes | 0 reflows during task but blocks response | Delayed paint due to blocking | [X] Bad |
| Asynchronous task with polling or WebSocket | Incremental DOM updates on status change | 1 reflow per update | Smooth paint with small incremental changes | [OK] Good |
AsyncResult to track task status and results using the task ID.AsyncResult is the standard way to check if a task is pending, running, or finished.AsyncResult instance for a task with ID stored in task_id?AsyncResult class is instantiated by passing the task ID as the first argument.AsyncResult(task_id) correctly creates the instance. Methods like .get() or .fetch() are not constructors.result = AsyncResult('abc123')
status = result.status
output = result.resultstatus and output represent if the task is still running?result.status remains 'PENDING'.result.result returns None until the task completes.result = AsyncResult(task_id)
if result.status == 'SUCCESS':
print(result.result)
else:
print('Task not done')result.status equals 'SUCCESS' to print the result.AsyncResult will not find the task and status stays 'PENDING' or similar.result.get() with a timeout waits for completion and raises exceptions on failure.result.get() in try-except catches task failures and allows logging errors safely.