Bird
0
0

How can you define a Celery task that retries automatically on failure up to 3 times with a 5-second delay between tries?

hard📝 Application Q9 of 15
Flask - Background Tasks
How can you define a Celery task that retries automatically on failure up to 3 times with a 5-second delay between tries?
ASet <code>max_retries=3</code> in Flask app config and use <code>@celery_app.task</code> normally.
BManually call the task again 3 times inside the function.
CUse <code>@celery_app.task(retry=True, retries=3)</code> without extra code.
DUse <code>@celery_app.task(bind=True, max_retries=3)</code> and call <code>self.retry(countdown=5)</code> inside the task on exception.
Step-by-Step Solution
Solution:
  1. Step 1: Use bind=True to access task instance

    This allows calling self.retry() inside the task.
  2. Step 2: Configure retries and countdown

    Set max_retries=3 in decorator and call self.retry(countdown=5) on exceptions to retry with delay.
  3. Final Answer:

    Use @celery_app.task(bind=True, max_retries=3) and call self.retry(countdown=5) inside the task on exception. -> Option D
  4. Quick Check:

    Bind task and call self.retry() for retries [OK]
Quick Trick: Use bind=True and self.retry() for retries [OK]
Common Mistakes:
MISTAKES
  • Setting retries only in Flask config
  • Using invalid decorator arguments
  • Manually looping retries inside task

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes