Prompt
Celery retry failed tasks
Latest observation
Oct 24, 2026
OpenAI APIWeb search: off
In Celery, “retrying failed tasks” usually means using Task.retry() inside the task when you catch an exception, or enabling automatic retries with autoretry_for.
1) Manual retry inside a task
from celery import shared_task
@shared_task(bind=True, max_retries=5, default_retry_delay=60)
def fetch_data(self, url):
try:
# do work that may fail
...
except Exception as exc:
raise self.retry(exc=exc)
Notes
bind=Trueis required so you can accessself.max_retries=5limits retries.default_retry_delay=60retries after 60 seconds.raise self.retry(...)is important: it tells Celery to re-queue the task.
2) Auto-retry for specific exceptions
from celery import shared_task
@shared_task(
autoretry_for=(TimeoutError, ConnectionError),
retry_kwargs={"max_retries": 5},
retry_backoff=True,
retry_jitter=True,
)
def fetch_data(url):
...
What this does
- Retries only for the exceptions listed in
autoretry_for retry_backoff=Trueadds exponential backoffretry_jitter=Trueadds randomness to avoid retry storms
3) Handling retries based on result state
If a task fails and is not retried, Celery marks it as FAILURE. If it’s retried, the task state becomes RETRY until it succeeds or exceeds max_retries.
4) Common pattern for transient errors
from celery import shared_task
import requests
@shared_task(bind=True, max_retries=3)
def download_file(self, url):
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
return r.text
except requests.RequestException as exc:
raise self.retry(exc=exc, countdown=30)
5) Retry from outside the task
If you already have an AsyncResult, you generally can’t “resubmit the same failed task” directly from the result object. Instead, you typically call the task again:
download_file.delay(url)
Or, if you want the same task id/workflow control, use Celery canvas primitives or custom logic.
6) Important caveats
- Retries can cause duplicate side effects if the task is not idempotent.
- Make sure your task can safely run more than once.
- Use exponential backoff for unstable external services.
- Don’t retry on permanent errors (e.g., validation errors, bad input).
If you want, I can show:
- how to retry all failed tasks globally, or
- how to configure Celery beat / worker settings for retries.