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.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install eb455e68-371c-11f1-9bc6-00163e2b0d79 --target codexÀ exécuter après confirmation du plan en dry-run.
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.
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Fil de discussion
Actifs similaires
Asynq — Simple Distributed Task Queue for Go
Asynq is a Go library for queueing and processing background tasks backed by Redis, with support for scheduling, retries, priorities, and a built-in web UI for monitoring.
Dramatiq — Fast Distributed Task Queue for Python
A reliable, high-performance background task processing library for Python with support for RabbitMQ and Redis brokers, automatic retries, rate limiting, and built-in result storage.
Huey — Lightweight Python Task Queue with Minimal Dependencies
Huey is a small but capable Python task queue supporting Redis, SQLite, and in-memory backends for scheduling periodic tasks, retries, and result storage with minimal configuration.
Machinery — Distributed Task Queue for Go
An asynchronous task queue and worker pool library for Go inspired by Celery, supporting Redis and AMQP backends with task chaining, grouping, and scheduled execution.