Redis

How to Use Redis with Celery

Celery Project or Celery is a simple, highly-scalable distributed system that provides a flexible mechanism to implement task queues in real-time.

This simple guide will cover the bare bones of implementing Celery with Python.

Requirements

Install the latest version of Python 3 and Redis server.

Basic knowledge in Redis and Python.

Installing

To use Celery with Redis, you need to install dependencies. We can do this by installing the Redis bundle as shown in the command below:

$ pip3 install -U celery[redis]

Configure Celery

The next step is to create a file that will hold the source code for our app. You can name the file with any term or label you see fit.

$ mkdir redis-celery
$ cd redis-celery

Create the source file.

$ touch main.py

Edit the file with your text editor and add the code.

from celery import Celery
BROKER_URL = "redis://:[email protected]:6379/0"
app = Celery('main', BROKER_URL)
@app.main
def maximum(x, y):
    if x > y:
        return x
    else:
        return y

In the code shown above, start by importing the required modules.

Next, we set the BROKER_URL which holds the URL to the Redis database.

The URL follows the format shown below:

redis://:password@host:port/db_index

Then, we create an instance of the Celery class and pass the current module and URL as the parameters.

We also create a simple function that returns a maximum of two numbers.

Run the worker as:

$ python3 -m celery -A main worker -l INFO

Call Task

Call the task using the delay method as:

from main import maximum
add.delay(100,30)

Running the task returns an Asynchronous result that you can use to check the task’s status or get the result.

Conclusion

This short article illustrates how to use the Celery project with Redis by covering the basic facts of implementing Celery with Python. Check the docs for more impressive and easy guide articles and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list