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:
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.
$ cd redis-celery
Create the source file.
Edit the file with your text editor and add the code.
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:
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:
Call Task
Call the task using the delay method as:
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.