Using Celery in Python A Comprehensive Guide

Celery is a powerful task queue implementation in Python that enables the execution of asynchronous, distributed tasks. It is highly configurable and extensible, making it suitable for a wide range of applications, including web development, data processing, and machine learning. In this article, we will explore how to use Celery to improve the performance and reliability of your Python applications.
Table of Contents
Section titled “Table of Contents”- Introduction to Celery
- Setting up Celery
- Creating and Running Tasks
- Task Retries and Error Handling
- Task Prioritization and Routing
- Monitoring and Managing Tasks
- Conclusion
Introduction to Celery
Section titled “Introduction to Celery”Celery is an asynchronous task queue that allows you to distribute tasks across multiple worker processes or even across multiple machines. It is built on top of the RabbitMQ, Redis, or Amazon SQS message broker, which enables efficient, reliable communication between the different components of your application.
Some common use cases for Celery include:
- Offloading time-consuming tasks from your main application thread to improve responsiveness
- Distributing tasks evenly among multiple worker processes to increase throughput
- Ensuring the reliable execution of tasks by handling errors and retries
Setting up Celery
Section titled “Setting up Celery”To get started with Celery, you’ll first need to install it using pip:
pip install celeryNext, you’ll need to choose a message broker. In this guide, we’ll use Redis, but you can also use RabbitMQ or Amazon SQS if you prefer. To install Redis, simply run:
pip install redisNow, you can create a new Python file (e.g., celery_app.py) and configure your Celery instance:
from celery import Celery
app = Celery('myapp', broker='redis://localhost:6379/0')
if __name__ == '__main__': app.start()Replace 'redis://localhost:6379/0' with the connection URL for your chosen message broker. If you’re using RabbitMQ, the URL would look like 'amqp://guest:guest@localhost:5672//'.
Creating and Running Tasks
Section titled “Creating and Running Tasks”A Celery task is simply a Python function decorated with the @app.task decorator. Here’s a simple example:
from celery import Celery
app = Celery('myapp', broker='redis://localhost:6379/0')
@app.taskdef add(x, y): return x + yTo call this task asynchronously, you can use the delay() method:
result = add.delay(4, 4)This method returns an AsyncResult object that you can use to check the status of the task or retrieve its result:
if result.ready(): print('The result is:', result.result)else: print('The task is still running')Task Retries and Error Handling
Section titled “Task Retries and Error Handling”Celery provides built-in support for task retries and error handling. To enable retries for a task, you can use the autoretry_for and retry_kwargs options:
from requests.exceptions import RequestException
@app.task(autoretry_for=(RequestException,), retry_kwargs={'max_retries': 5, 'countdown': 60})def fetch_url(url): response = requests.get(url) response.raise_for_status() return response.textIn this example, the fetch_url task will automatically retry up to 5 times if a RequestException occurs, with a 60-second delay between retries.
You can also handle errors manually by catching exceptions and calling the retry() method:
from requests.exceptions import RequestException
@app.task(bind=True, max_retries=5)def fetch_url(self, url): try: response = requests.get(url) response.raise_for_status() return response.text except RequestException as e: self.retry(exc=e, countdown=60)Task Prioritization and Routing
Section titled “Task Prioritization and Routing”Celery allows you to prioritize tasks and route them to specific worker processes or queues. You can assign a priority to a task using the priority option:
@app.task(priority=10)def important_task(): passYou can also route tasks to specific queues using the queue option:
@app.task(queue='myqueue')def myqueue_task(): ## ...To start a worker process that listens to a specific queue, use the -Q option:
celery -A celery_app worker --loglevel=info -Q myqueueMonitoring and Managing Tasks
Section titled “Monitoring and Managing Tasks”Celery provides several tools for monitoring and managing tasks, including the following:
Flower
Section titled “Flower”Flower is a web-based tool for monitoring and administering Celery tasks. To install and run Flower, simply run:
pip install flowercelery -A celery_app flowerBy default, Flower will be available at http://localhost:5555.
Celery CLI
Section titled “Celery CLI”The Celery command line interface (CLI) can be used to inspect and manage tasks, workers, and queues. Some common commands include:
celery -A celery_app call: Call a task by namecelery -A celery_app inspect active: List active taskscelery -A celery_app inspect reserved: List reserved taskscelery -A celery_app inspect scheduled: List scheduled taskscelery -A celery_app control cancel: Cancel a task by IDcelery -A celery_app control pool: Manage worker processes
For a full list of commands, consult the Celery documentation.
Conclusion
Section titled “Conclusion”In this article, we have explored the basics of using Celery in Python, including setting up Celery, creating and running tasks, handling errors and retries, prioritizing and routing tasks, and monitoring and managing tasks. By integrating Celery into your Python applications, you can improve their performance, reliability, and scalability.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js