Complete the code to retry a task after failure using Celery's retry method.
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)
The retry method is used inside a Celery task to retry the task after an exception.
Complete the code to catch a specific exception and retry the task.
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)
We catch FileNotFoundError to retry when the file is missing.
Fix the error in the task to properly handle retries with max retries limit.
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])
The countdown sets the delay before retry. 5 seconds is a reasonable retry delay.
Fill both blanks to catch a timeout error and retry after 15 seconds.
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])
We catch requests.exceptions.Timeout and retry after 15 seconds.
Fill all three blanks to create a task that retries on ValueError with max 4 retries and 20 seconds delay.
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])
The task retries up to 4 times on ValueError with 20 seconds delay between retries.