0
0
Djangoframework~10 mins

Task retry and error handling in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task retry and error handling
Start Task
Try to execute task
Task succeeds?
YesFinish Task
No
Check retry count < max retries?
Yes
Wait or schedule retry
Retry task
Back to Try to execute task
Handle failure (log, alert)
Finish Task with error
Back to Try to execute task
The task tries to run. If it fails, it checks if retries remain. If yes, it retries. If no, it handles the error and stops.
Execution Sample
Django
from celery import shared_task

@shared_task(bind=True, max_retries=3)
def send_email(self, email):
    try:
        # send email logic
        pass
    except Exception as exc:
        raise self.retry(exc=exc, countdown=5)
A Celery task tries to send an email, retries up to 3 times on failure with 5 seconds delay.
Execution Table
AttemptActionException Raised?Retry CountNext Step
1Try sending emailYes: ConnectionError0Schedule retry after 5s
2Retry sending emailYes: TimeoutError1Schedule retry after 5s
3Retry sending emailYes: SMTPServerError2Schedule retry after 5s
4Retry sending emailYes: SMTPServerError3Max retries reached, handle failure
EndStop retriesN/A3Log error and alert
💡 Max retries (3) reached, task stops retrying and handles failure.
Variable Tracker
VariableStartAfter Attempt 1After Attempt 2After Attempt 3After Attempt 4
retry_count01233
exceptionNoneConnectionErrorTimeoutErrorSMTPServerErrorSMTPServerError
task_statePendingRetryingRetryingRetryingFailed
Key Moments - 3 Insights
Why does the task retry only 3 times and then stop?
Because max_retries is set to 3, so after the third retry (see execution_table row 4), the task stops retrying and handles the failure.
What happens if the task succeeds before max retries?
If the task succeeds, it finishes immediately without retrying (not shown in this trace but would skip retries).
Why do we use 'self.retry' inside the except block?
'self.retry' raises a retry exception that tells Celery to retry the task later, increasing retry_count and scheduling the next attempt.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the retry_count after the second attempt?
A0
B2
C1
D3
💡 Hint
Check the 'Retry Count' column in row for Attempt 2 in execution_table.
At which attempt does the task stop retrying and handle failure?
AAttempt 4
BAttempt 2
CAttempt 3
DAttempt 5
💡 Hint
Look at the 'Next Step' column where it says 'Max retries reached' in execution_table.
If max_retries was set to 5, how would the execution_table change?
AThe task would retry only 3 times as before.
BThe task would retry 5 times before failure.
CThe task would never retry.
DThe task would retry only once.
💡 Hint
max_retries controls how many retries happen before stopping, see concept_flow and execution_table.
Concept Snapshot
Task retry and error handling in Django with Celery:
- Use @shared_task(bind=True, max_retries=N) to set retries.
- Wrap task code in try-except.
- On exception, call self.retry(exc=exc, countdown=seconds).
- Task retries up to max_retries times.
- After max retries, handle failure (log, alert).
- Helps recover from temporary errors automatically.
Full Transcript
This visual trace shows how a Django Celery task handles retries and errors. The task tries to send an email. If it fails, it raises an exception caught by the except block. The task then calls self.retry to schedule a retry after 5 seconds. The retry count increases each time. After 3 retries, the task stops retrying and handles the failure by logging or alerting. Variables like retry_count and exception change with each attempt. This pattern helps tasks recover from temporary problems by retrying automatically, improving reliability.