Django-RQ

4.1.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to configure Django-RQ, define a background task using the `@job` decorator, and enqueue it. It includes a minimal Django settings setup for standalone execution. To run this, you need a running Redis server and then execute the script, followed by `python manage.py rqworker default` in a separate terminal to process the job. [2, 3, 6]

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)

view raw JSON →