A simple app that use Django
and Redis
together to queueing jobs and processing them in the background.
This project uses django-rq
which is a simple app that allows you to confiure your queues in django. django-rq
internally uses RQ.
In layman language RQ
is an alternative of celery.
There are many ways to run async tasks in python. Most of the companies uses celery
, since it's pretty famous also. But, here we are, now looking at an alternative of celery which is pretty easy to setup as well RQ
.
Let's go through a couple of difference between celery
and RQ
:
-
RQ
is known for it's simplicity whereasCelery
is known for it robustness. -
RQ
usesredis
as a message broker, whereasCelery
suports bothredis
andrabbitMQ
.Celery
has clearly won here.
There are other differences as well, why don't you explore those on your own :)
IMHO, celery
is awesome ;)
Knowledge of Django and zeal to learn :)
python3 -m venv .venv
source ./.venv/bin/activate
git clone https://github.com/nightwarriorftw/blackpiper.git
pip install -r requirements.txt
RQ
uses redis
as a message broker. So, it's important to have redis installed on your system.
In case you don't have redis
installed on your system. You can follow this to install redis
on your system.
-
Create a
.env
file in the root folder of the project. -
Add the following variables in your
.env
file :SECRET_KEY="<your django secret key>" DEBUG=TRUE RQ_QUEUE_HOST="localhost" RQ_QUEUE_PORT=6379 RQ_QUEUE_DB=0 RQ_QUEUE_DEFAULT_TIMEOUT=360 RQ_QUEUE_PASSWORD="<your redis password>"
Add the following in your project's settings.py
# RQ Configurations
RQ_QUEUES = {
"default":
{
"HOST": env("RQ_QUEUE_HOST"),
"PORT": env("RQ_QUEUE_PORT"),
"DB": env("RQ_QUEUE_DB"),
"DEFAULT_TIMEOUT": env("RQ_QUEUE_DEFAULT_TIMEOUT"),
"PASSWORD": env("RQ_QUEUE_PASSWORD")
},
}
# RQ Logging Configuration
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler",},},
"root": {"handlers": ["console"], "level": "DEBUG",},
}
You can change your logging
configuration depending upon your usecase. Like, there are hell lot of options you can find here.
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Open another new terminal and run the following command
python manage.py rqworker default
The above command will start rq default worker. There are lot of ways you can configure your rq workers. Change your RQ
configuration depending upon your usecase. Refer, django-rq
docs for this :)
-
Open the shell
python manage.py shell
-
Run
hello
method to test if everything is working on notfrom piper.tasks import hello hello.delay()
You will find the call will return a reference of async method. You can check your terminal/console to validate if your setup is complete.
Do read the django-rq
and RQ
docs. It's pretty awesome :)
-
Github: nightwarriorftw
-
Linkedin: nightwarriorftw
-
Twitter: nightwarriorftw
Credits goes to me