Celery 2
Celery 2
#celery
Table of Contents
About 1
Remarks 2
Examples 2
Installation or Setup 2
Celery + Redis 2
Installation 2
Configuration 2
Application 3
Keeping Results 3
Examples 5
Credits 6
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: celery
It is an unofficial and free celery ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official celery.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with celery
Remarks
"Celery is an asynchronous task queue/job queue based on distributed message passing." –
http://www.celeryproject.org/
Celery is great for asychronous and scheduled background tasks. It is commonly used for long-
running tasks that are part of a Django or Flask application.
Examples
Installation or Setup
You can install Celery either via the Python Package Index (PyPI) or from source.
$ easy_install celery
Celery + Redis
Installation
Additional dependencies are required for Redis support. Install both Celery and the dependencies
in one go using the celery[redis] bundle:
https://riptutorial.com/ 2
Configuration
Configure the location of your Redis database:
BROKER_URL = 'redis://localhost:6379/0'
redis://:password@hostname:port/db_number
Application
Create the file tasks.py:
BROKER_URL = 'redis://localhost:6379/0'
app = Celery('tasks', broker=BROKER_URL)
@app.task
def add(x, y):
return x + y
The first argument to Celery is the name of the current module. This way names can be
automatically generated. The second argument is the broker keyword which specifies the URL of
the message broker.
Calling a task returns an AsyncResult instance, which can check the state of the task, wait for the
task to finish, or get its return value. (If the task failed, it gets the exception and traceback).
https://riptutorial.com/ 3
Keeping Results
To keep track of the task's states, Celery needs to store or send the states somewhere. Use Redis
as the result backend:
BROKER_URL = 'redis://localhost:6379/0'
BACKEND_URL = 'redis://localhost:6379/1'
app = Celery('tasks', broker=BROKER_URL, backend=BACKEND_URL)
Now with the result backend configured, call the task again. This time hold on to the AsyncResult
instance returned from the task:
The ready() method returns whether the task has finished processing or not:
>>> result.ready()
False
It is possible to wait for the result to complete, but this is rarely used since it turns the
asynchronous call into a synchronous one:
>>> result.get(timeout=1)
8
https://riptutorial.com/ 4
Chapter 2: Celery monitoring with Flower
Examples
Up and running with Flower
http://localhost:5555
https://riptutorial.com/ 5
Credits
S.
Chapters Contributors
No
Celery monitoring
2 ettanany
with Flower
https://riptutorial.com/ 6