Celery in Python: background tasks, retries, routing and monitoring

Celery is a distributed task queue for Python. Your application sends a message describing a function call to a broker (usually Redis or RabbitMQ), and one or more worker processes pick it up and run it. Use it for work that shouldn’t block a web request: sending email, resizing images, calling slow third-party APIs, or periodic jobs.
The examples were checked against Celery 5.7, which requires Python 3.10 or later.
Install Celery with the Redis extras (plus requests, which the retry examples use). The extras install the redis Python client, but you still need a Redis server running:
pip install "celery[redis]" requestsdocker run -d -p 6379:6379 redisCreate tasks.py:
import requestsfrom celery import Celery
app = Celery( "tasks", broker="redis://localhost:6379/0", backend="redis://localhost:6379/1",)
@app.taskdef add(x, y): return x + yThe broker carries task messages. The backend stores return values. Without a backend you can still send tasks, but you can’t read their results.
Start a worker from the same directory:
celery -A tasks worker --loglevel=INFOCelery doesn’t officially support Windows. If you’re testing locally on Windows, add --pool=solo, because the default prefork pool doesn’t work there.
For RabbitMQ, the broker URL looks like amqp://guest:guest@localhost:5672//.
Calling tasks
Section titled “Calling tasks”delay() sends the task and returns an AsyncResult immediately:
from tasks import add
result = add.delay(4, 4)print(result.id)print(result.get(timeout=10)) # blocks until the worker finishes: 8result.ready() checks without blocking. apply_async() is the full form of delay() and takes options like countdown, eta, queue and priority:
add.apply_async((4, 4), countdown=30) # run no earlier than 30 seconds from nowRetries
Section titled “Retries”For transient errors, let Celery retry automatically:
@app.task( autoretry_for=(requests.RequestException,), retry_backoff=True, retry_kwargs={"max_retries": 5},)def fetch_url(url): response = requests.get(url, timeout=10) response.raise_for_status() return response.textretry_backoff=True waits 1s, 2s, 4s and so on between attempts (with jitter), up to retry_backoff_max (600 seconds by default).
For more control, bind the task and call self.retry() yourself. retry() raises an exception to tell the worker to reschedule the task, so raise it:
@app.task(bind=True, max_retries=5)def fetch_url_manual(self, url): try: response = requests.get(url, timeout=10) response.raise_for_status() return response.text except requests.RequestException as exc: raise self.retry(exc=exc, countdown=60)Tasks that may be retried, or redelivered after a worker crash, should be idempotent: running them twice must not do the work twice.
Routing to queues
Section titled “Routing to queues”Send slow tasks to their own queue so they don’t hold up quick ones. Configure the routing on the app:
app.conf.task_routes = { "tasks.generate_report": {"queue": "reports"},}
@app.taskdef generate_report(report_id): ...You can also choose a queue per call with generate_report.apply_async((42,), queue="reports"). Then start a worker that consumes that queue:
celery -A tasks worker --loglevel=INFO -Q reportsA worker started without -Q only consumes the default celery queue, so tasks routed to reports sit in Redis until a worker listens there.
Priorities
Section titled “Priorities”Priorities exist, but they behave differently per broker:
- RabbitMQ: higher numbers run first. The queue must be declared with
x-max-priority, for example viaapp.conf.task_queue_max_priority = 10. - Redis: 0 is the highest priority and 9 the lowest. By default, the 10 levels collapse into 4 internal lists. Redis has no native priorities, so the ordering is approximate.
add.apply_async((2, 2), priority=0)In practice, separate queues with dedicated workers are usually more predictable than priorities.
Monitoring
Section titled “Monitoring”The inspect and control commands talk to running workers:
celery -A tasks inspect active # tasks being executed nowcelery -A tasks inspect reserved # prefetched, waiting to runcelery -A tasks inspect scheduled # tasks with an ETA/countdowncelery -A tasks call tasks.add -a '[2, 3]'celery -A tasks control revoke <task_id>celery -A tasks control pool_grow 2Flower is a web dashboard for workers and tasks:
pip install flowercelery -A tasks flowerIt listens on http://localhost:5555 by default. Don’t expose it publicly without authentication, because it can revoke tasks and shut down workers.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)