Getting started with celery
"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.
Celery + Redis
Additional dependencies are required for Redis support. Install both Celery and the dependencies in one go using the
Configure the location of your Redis database:
The URL should be in the format of:
Create the file tasks.py:
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.
Running the celery worker server
Run the worker by executing with the worker argument:
Calling the task
To call the task, use the
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).
To keep track of the task's states, Celery needs to store or send the states somewhere. Use Redis as the result backend:
To read more about result backends please see Result Backends.
Now with the result backend configured, call the task again. This time hold on to the AsyncResult instance returned from the task:
ready() method returns whether the task has finished processing or not:
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:
Based on celery official document
Installation or Setup
You can install Celery either via the Python Package Index (PyPI) or from source.
To install the latest version using
To install using
Downloading and installing from source
Download the latest version of Celery from http://pypi.python.org/pypi/celery/
You can install it by doing the following: