0
0
Djangoframework~10 mins

Task retry and error handling in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to retry a task after failure using Celery's retry method.

Django
from celery import shared_task

@shared_task(bind=True)
def fetch_data(self):
    try:
        # code that might fail
        pass
    except Exception as e:
        raise self.[1](exc=e, countdown=5)
Drag options to blanks, or click blank then click option'
Aabort
Bfail
Cretry
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fail' or 'abort' instead of 'retry' causes the task to stop immediately.
Not using 'self.' before the method causes an error.
2fill in blank
medium

Complete the code to catch a specific exception and retry the task.

Django
from celery import shared_task

@shared_task(bind=True)
def process_file(self, filename):
    try:
        # process the file
        pass
    except [1] as e:
        raise self.retry(exc=e, countdown=10)
Drag options to blanks, or click blank then click option'
ATypeError
BValueError
CKeyError
DFileNotFoundError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception causes the retry not to trigger.
Using a generic exception hides specific errors.
3fill in blank
hard

Fix the error in the task to properly handle retries with max retries limit.

Django
from celery import shared_task

@shared_task(bind=True, max_retries=3)
def send_email(self, user_id):
    try:
        # send email code
        pass
    except Exception as e:
        raise self.retry(exc=e, countdown=[1])
Drag options to blanks, or click blank then click option'
A5
B10
C60
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero causes immediate retry and possible overload.
Using too large values delays retries unnecessarily.
4fill in blank
hard

Fill both blanks to catch a timeout error and retry after 15 seconds.

Django
from celery import shared_task
import requests

@shared_task(bind=True)
def fetch_url(self, url):
    try:
        response = requests.get(url, timeout=5)
    except [1] as e:
        raise self.retry(exc=e, countdown=[2])
Drag options to blanks, or click blank then click option'
Arequests.exceptions.Timeout
Brequests.exceptions.ConnectionError
C10
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception causes no retry.
Using wrong countdown delays retries incorrectly.
5fill in blank
hard

Fill all three blanks to create a task that retries on ValueError with max 4 retries and 20 seconds delay.

Django
from celery import shared_task

@shared_task(bind=True, max_retries=[1])
def calculate(self, data):
    try:
        # calculation code
        pass
    except [2] as e:
        raise self.retry(exc=e, countdown=[3])
Drag options to blanks, or click blank then click option'
A4
BValueError
C20
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception type prevents retry.
Setting max_retries too low or countdown too short.