Django Celery Task Track Fix
In this tutorial, you'll learn about Django Celery Task Track Fix. We cover key concepts, practical examples, and best practices.
The Problem
You launch a Celery task but have no idea if it succeeded, failed, or is still running. Without result tracking, debugging async failures is difficult.
Quick Fix
Wrong — no result backend
# settings.py
CELERY_RESULT_BACKEND = None
# views.py
def start_task(request):
my_task.delay() # Task runs, but we never know the result
return Response({'status': 'started'})
Output: Task starts but status, result, and traceback are invisible.
Correct — Redis result backend
# settings.py
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_EXTENDED = True
# tasks.py
@app.task(bind=True)
def long_task(self, arg):
self.update_state(state='PROGRESS', meta={'progress': 0})
# ... do work ...
self.update_state(state='PROGRESS', meta={'progress': 50})
# ... do work ...
return {'result': 'done', 'progress': 100}
Output: Task status is stored in Redis and queryable by task ID.
Checking task status
from celery.result import AsyncResult
def task_status(request, task_id):
result = AsyncResult(task_id)
return Response({
'task_id': task_id,
'state': result.state,
'status': result.status,
'result': result.result,
'traceback': result.traceback,
})
Polling from frontend
# views.py
def start_and_poll(request):
task = my_task.delay()
return Response({
'task_id': task.id,
'status_url': f'/tasks/{task.id}/status/',
})
# JavaScript (client-side polling)
"""
const resp = await fetch('/api/start-task/');
const { task_id, status_url } = await resp.json();
const interval = setInterval(async () => {
const status = await fetch(status_url);
const data = await status.json();
if (data.state === 'SUCCESS' || data.state === 'FAILURE') {
clearInterval(interval);
}
}, 2000);
"""
Task success/failure callbacks
@app.task(bind=True, on_success=handle_success, on_failure=handle_failure)
def my_task(self):
...
def handle_success(self, retval, task_id, args, kwargs):
notify_success(task_id, retval)
def handle_failure(self, exc, task_id, args, kwargs, einfo):
notify_failure(task_id, str(exc))
Prevention
- Always set
CELERY_RESULT_BACKENDin production. - Store task IDs and expose status endpoints for long-running tasks.
- Use
CELERY_TASK_TRACK_STARTED = Trueto see tasks currently running.
Common Mistakes with celery task track
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world DJANGO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro