Celery — Distributed Task Queue for Python
Celery is the most popular distributed task queue for Python. It processes millions of tasks per day at companies worldwide, handling background jobs, scheduled tasks, and real-time processing with support for multiple message brokers and result backends.
What it is
Celery is the most widely used distributed task queue for Python. It handles background job processing, scheduled periodic tasks, and real-time message processing with support for multiple message brokers (RabbitMQ, Redis, Amazon SQS) and result backends (Redis, PostgreSQL, MongoDB).
It targets Python developers who need to offload time-consuming work (sending emails, processing images, running ML inference, calling external APIs) from their web request cycle into asynchronous background workers.
How it saves time or tokens
Without Celery, long-running operations block web requests, causing timeouts and poor user experience. Celery moves these operations to separate worker processes that execute independently. The web application returns immediately while the task runs in the background. Celery Beat handles periodic scheduling, replacing cron jobs with a more manageable, code-defined system.
How to use
- Install Celery:
pip install celery[redis](with Redis as both broker and result backend). - Define tasks as decorated functions with
@app.task. - Start a worker process with
celery -A myapp workerand call tasks with.delay()or.apply_async().
Example
# tasks.py
from celery import Celery
app = Celery('myapp', broker='redis://localhost:6379/0',
backend='redis://localhost:6379/1')
@app.task
def send_email(to, subject, body):
# Simulate email sending
import time
time.sleep(5)
return f'Email sent to {to}'
# In your web application:
result = send_email.delay('user@example.com', 'Hello', 'World')
print(result.id) # Task ID for tracking
print(result.get(timeout=30)) # Wait for result
Related on TokRepo
- Automation tools — Task and workflow automation
- Coding tools — Python development frameworks
Common pitfalls
- Celery tasks must be serializable. Avoid passing database connections, file handles, or other non-serializable objects as task arguments.
- Memory leaks in long-running workers accumulate over time. Use the --max-tasks-per-child flag to restart workers after a set number of tasks.
- Celery Beat (the periodic task scheduler) should run as a single instance. Running multiple Beat processes causes duplicate task execution.
Frequently Asked Questions
RabbitMQ is the recommended production broker. It provides reliable message delivery, supports acknowledgments, and handles high throughput. Redis works well for simpler setups and doubles as a result backend. Amazon SQS is suitable for AWS deployments.
Flower is the most popular Celery monitoring tool. It provides a web dashboard showing active workers, task history, success/failure rates, and resource usage. Install it with pip install flower and run it alongside your workers.
Yes. Celery Beat is the built-in periodic task scheduler. Define schedules in your Celery configuration using crontab expressions or timedelta intervals. Beat sends tasks to the queue at the specified times, and workers process them normally.
Celery supports automatic retries with configurable delays and maximum retry counts. Use the retry_backoff and max_retries parameters on tasks. Failed tasks can also be routed to a dead-letter queue for manual inspection.
Celery handles near-real-time processing well for tasks that take seconds to minutes. For sub-millisecond latency requirements, a message streaming platform like Kafka is more appropriate. Celery excels at background job processing and scheduled task execution.
Citations (3)
- Celery GitHub Repository— Celery is the most popular distributed task queue for Python
- Celery Official Docs— Celery documentation and getting started
- RabbitMQ Tutorials— RabbitMQ as a message broker
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.