Django Dramatiq

0.15.0 · active · verified Fri Apr 17

django-dramatiq is a Django application that seamlessly integrates the Dramatiq task queue library with Django projects. It simplifies broker configuration, task discovery, and provides Django management commands for running workers. The current version is 0.15.0 and it maintains an active release cadence, with several updates per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and enqueue a Dramatiq task within a Django environment configured by django-dramatiq. It assumes you have Redis running at `localhost:6379`. For a real project, replace the minimal settings configuration with your actual `settings.py` and `manage.py` structure. The task is defined using `dramatiq.actor`, which automatically uses the broker configured via `DRAMATIQ_BROKER` in your Django settings. Remember to run `python manage.py rundramatiq` in a separate process to start the worker that will process these tasks.

import os
from django.conf import settings
from django.apps import apps
from django.core.management import call_command

# Minimal Django setup for demonstration
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

if not apps.ready:
    settings.configure(
        INSTALLED_APPS=[
            'django_dramatiq',
            'myapp'
        ],
        DRAMATIQ_BROKER={
            "URL": os.environ.get('DRAMATIQ_BROKER_URL', 'redis://localhost:6379/0'),
            "OPTIONS": {
                "decode_responses": True,
            },
        },
        SECRET_KEY='a-very-secret-key',
        DEBUG=True,
        # Add other minimal settings if necessary
    )
    apps.populate(settings.INSTALLED_APPS)


# --- myapp/tasks.py ---
from dramatiq import dramatiq
import time

@dramatiq.actor
def my_task(x, y):
    print(f"Executing task: {x} + {y}")
    time.sleep(1) # Simulate work
    result = x + y
    print(f"Task finished: {x} + {y} = {result}")
    return result


# --- How to use the task ---
print("Enqueuing task...")
my_task.send(5, 3)
print("Task enqueued. Run `python manage.py rundramatiq` in a separate terminal to process it.")

# In a real Django project, you'd call my_task.send() from a view or signal handler.
# To run the worker (in a separate shell):
# python manage.py rundramatiq

view raw JSON →