Performance: Task retry and error handling
This affects backend task execution speed and user experience by controlling how failed tasks are retried and errors are handled.
Jump into concepts and practice - no test required
from celery import shared_task @shared_task(bind=True, max_retries=3, default_retry_delay=10) def process_task(self): try: # task logic pass except Exception as exc: raise self.retry(exc=exc)
from time import sleep def process_task(): try: # task logic pass except Exception: sleep(10) # naive retry delay process_task() # recursive retry without limit
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking recursive retry | 0 | 0 | 0 | [X] Bad |
| Asynchronous retry with limits (Celery) | 0 | 0 | 0 | [OK] Good |
@app.task(bind=True, max_retries=3)
def fetch_data(self):
try:
# code that may fail
raise ValueError('Temporary error')
except Exception as exc:
raise self.retry(exc=exc, countdown=5)@app.task(bind=True)
def process_data():
try:
# risky operation
pass
except Exception as e:
self.retry(exc=e, countdown=10)