Django-RQ
Django-RQ is a simple app that provides seamless integration for RQ (Redis Queue) with the Django framework. It allows developers to configure Redis queues directly within Django's settings, enqueue tasks, and manage background workers efficiently. The library is actively maintained, currently at version 4.1.0, and typically releases updates as new features or compatibility fixes for Django and RQ are introduced.
Warnings
- breaking In version 4.0, Django-RQ automatically integrates with Django's admin backend, removing the need for manual URL configuration. The `AUTOCOMMIT` setting was also replaced by `COMMIT_MODE`. Legacy Sentry integration was removed.
- breaking Version 2.0 introduced backward incompatible changes. `FailedQueue` was replaced by `FailedJobRegistry`.
- breaking RQ version 1.14 removed `use_connection()`, which caused issues with older versions of `django-rq`.
- gotcha When running `rqworker`, it must be executed within a Django context to properly access settings. If not using `python manage.py rqworker`, you might need to set the `DJANGO_SETTINGS_MODULE` environment variable.
- gotcha For optimal performance and to avoid issues, ensure your Redis instance is properly configured and accessible from both your Django application and RQ workers. Misconfigured Redis connections are a common source of problems.
Install
-
pip install django-rq
Imports
- job
from django_rq import job
- enqueue
import django_rq django_rq.enqueue(func, *args, **kwargs)
- get_queue
import django_rq queue = django_rq.get_queue('queue_name') - get_connection
import django_rq redis_conn = django_rq.get_connection('queue_name') - get_scheduler
import django_rq scheduler = django_rq.get_scheduler('queue_name')
Quickstart
import os
import time
from django.conf import settings
from django.apps import apps
# Minimal Django setup for demonstration
if not apps.ready:
settings.configure(
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_rq',
],
RQ_QUEUES={
'default': {
'HOST': os.environ.get('REDIS_HOST', 'localhost'),
'PORT': int(os.environ.get('REDIS_PORT', 6379)),
'DB': int(os.environ.get('REDIS_DB', 0)),
'PASSWORD': os.environ.get('REDIS_PASSWORD', None),
'DEFAULT_TIMEOUT': 360,
}
},
SECRET_KEY='a-very-secret-key',
DEBUG=True,
ROOT_URLCONF=__name__,
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
],
)
import django
django.setup()
from django_rq import job
import django_rq
@job
def my_background_task(arg1, arg2):
"""A simple task to be run in the background."""
print(f"Running task with {arg1} and {arg2}...")
time.sleep(2) # Simulate work
result = arg1 + arg2
print(f"Task finished. Result: {result}")
return result
# Enqueue the task
print("Enqueuing task...")
job_id = my_background_task.delay(10, 20)
print(f"Task enqueued with ID: {job_id}")
# To run the worker, you would typically use:
# python manage.py rqworker default
# (Ensure a Redis server is running)