Communitygithub.com

django-celery

Django + Celery asynchrone Aufgabenmuster – Konfiguration, Aufgabenentwurf, Zeitplanung, Wiederholungen, Canvas-Workflows, Überwachung und Tests. Verwenden Sie dies beim Hinzufügen von Hintergrundjobs, geplanten Aufgaben oder asynchroner Verarbeitung zu einer Django-App.

Was ist django-celery?

Dieses Skill bietet produktionsreife Muster für die asynchrone Aufgabenverarbeitung in Django mithilfe von Celery mit Redis oder RabbitMQ. Es umfasst die gesamte Einrichtung des Projekts, von der Installation und Konfiguration von Celery in der Django-Umgebung bis hin zum Starten von Workern und Beat-Schedulern. Die Dokumentation enthält detaillierte Entwurfsmuster für Aufgaben, darunter grundlegende, wiederholbare, idempotente und zeitlich begrenzte Aufgaben, sowie Best Practices für das Aufrufen von Aufgaben mit Verzögerung, Zeitplanung und Warteschlangen-Routing. Es behandelt auch fortgeschrittene Themen wie das Verketten und Gruppieren von Aufgaben mit Canvas, die Fehlerbehandlung mit Dead-Letter-Queues und die Integration von Sentry zur Überwachung von Aufgabenfehlern. Das Skill führt in das Testen von Celery-Aufgaben ein, sowohl mit Unit-Tests ohne Broker als auch mit Integrationstests, die 'CELERY_TASK_ALWAYS_EAGER' für synchrone Ausführung nutzen. Schließlich enthält es eine Produktions-Checkliste mit wichtigen Einstellungen und zeigt Anti-Patterns auf, die Entwickler vermeiden sollten.

Funktioniert mit~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/django-celery

In Ihrer bevorzugten KI fragen

Öffnet einen neuen Chat, in dem dieser Agent-Skill bereits geladen ist.

Dokumentation

Django + Celery Asynchrone Aufgabenmuster

Produktionsreife Muster für die Hintergrundaufgabenverarbeitung in Django mit Celery und Redis oder RabbitMQ.

Wann aktivieren

  • Hinzufügen von Hintergrundjobs oder asynchroner Verarbeitung zu einer Django-App
  • Implementieren von periodischen/geplanten Aufgaben
  • Auslagern langsamer Operationen (E-Mail, PDF-Generierung, API-Aufrufe) aus dem Request-Zyklus
  • Einrichten von Celery Beat für cron-ähnliche Zeitplanung
  • Debuggen von Aufgabenfehlern, Wiederholungen oder Warteschlangenrückständen
  • Schreiben von Tests für Celery-Aufgaben

Projekteinrichtung

Installation

pip install 'celery[redis]' django-celery-results django-celery-beat

celery.py — App-Einstiegspunkt

# config/celery.py
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.development')

app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()  # Findet tasks.py in jeder INSTALLED_APP

@app.task(bind=True, ignore_result=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
# config/__init__.py
from .celery import app as celery_app

__all__ = ('celery_app',)

Django-Einstellungen

# config/settings/base.py

# Broker (Redis wird für die Produktion empfohlen)
CELERY_BROKER_URL = env('CELERY_BROKER_URL', default='redis://localhost:6379/0')
CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND', default='django-db')

# Serialisierung
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

# Aufgabenverhalten
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60        # Hartes Limit: 30 Min
CELERY_TASK_SOFT_TIME_LIMIT = 25 * 60   # Weiches Limit: sendet SoftTimeLimitExceeded
CELERY_WORKER_PREFETCH_MULTIPLIER = 1   # Verhindert, dass Worker lange Aufgaben horten
CELERY_TASK_ACKS_LATE = True            # Aufgaben bei Worker-Absturz erneut in die Warteschlange

# Ergebnis-Persistenz
CELERY_RESULT_EXPIRES = 60 * 60 * 24   # Ergebnisse 24 Stunden aufbewahren

# Beat-Scheduler (für periodische Aufgaben)
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

# Installierte Apps
INSTALLED_APPS += [
    'django_celery_results',
    'django_celery_beat',
]

Worker starten

# Worker starten (Entwicklung)
celery -A config worker --loglevel=info

# Beat-Scheduler starten (periodische Aufgaben)
celery -A config beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler

# Kombinierter Worker + Beat (nur Entwicklung, niemals in Produktion)
celery -A config worker --beat --loglevel=info

# Produktion: mehrere Worker mit Parallelität
celery -A config worker --loglevel=warning --concurrency=4 -Q default,high_priority

Aufgabenentwurfsmuster

Grundlegende Aufgabe

# apps/notifications/tasks.py
from celery import shared_task
import logging

logger = logging.getLogger(__name__)

@shared_task(name='notifications.send_welcome_email')
def send_welcome_email(user_id: int) -> None:
    """Sende Willkommens-E-Mail an neu registrierten Benutzer."""
    from apps.users.models import User
    from apps.notifications.services import EmailService

    try:
        user = User.objects.get(pk=user_id)
    except User.DoesNotExist:
        logger.warning('send_welcome_email: Benutzer %s nicht gefunden', user_id)
        return  # Idempotent — nicht auslösen, Aufgabe bereits unmöglich auszuführen

    EmailService.send_welcome(user)
    logger.info('Willkommens-E-Mail an Benutzer %s gesendet', user_id)

Wiederholbare Aufgabe

@shared_task(
    bind=True,
    name='integrations.sync_to_crm',
    max_retries=5,
    default_retry_delay=60,       # Sekunden vor erster Wiederholung
    autoretry_for=(ConnectionError, TimeoutError),
    retry_backoff=True,           # Exponentielles Backoff
    retry_backoff_max=600,        # Begrenzung auf 10 Minuten
    retry_jitter=True,            # Zufälligkeit, um Herdenproblem zu vermeiden
)
def sync_contact_to_crm(self, contact_id: int) -> dict:
    """Synchronisiere Kontakt mit externem CRM mit Wiederholung bei vorübergehenden Fehlern."""
    from apps.crm.services import CRMClient

    try:
        result = CRMClient().sync(contact_id)
        return result
    except CRMClient.RateLimitError as exc:
        # Spezifische Wiederholungsverzögerung aus dem Antwortheader
        raise self.retry(exc=exc, countdown=int(exc.retry_after))

Idempotentes Aufgabenmuster

Entwerfen Sie Aufgaben so, dass sie mit denselben Eingaben sicher mehrmals ausgeführt werden können:

@shared_task(name='orders.mark_shipped')
def mark_order_shipped(order_id: int, tracking_number: str) -> None:
    """Markiere Bestellung als versendet — sicher mehrmals ausführbar."""
    from apps.orders.models import Order

    updated = Order.objects.filter(
        pk=order_id,
        status=Order.Status.PROCESSING,    # Sicherung: nur aktualisieren, wenn noch nicht versendet
    ).update(
        status=Order.Status.SHIPPED,
        tracking_number=tracking_number,
    )

    if not updated:
        logger.info('mark_order_shipped: Bestellung %s bereits versendet oder nicht gefunden', order_id)

Aufgabe mit weichem Zeitlimit

from celery.exceptions import SoftTimeLimitExceeded

@shared_task(
    bind=True,
    name='reports.generate_pdf',
    soft_time_limit=120,
    time_limit=150,
)
def generate_pdf_report(self, report_id: int) -> str:
    """Generiere PDF-Bericht mit anmutiger Zeitüberschreitungsbehandlung."""
    from apps.reports.services import PDFGenerator

    try:
        path = PDFGenerator.build(report_id)
        return path
    except SoftTimeLimitExceeded:
        # Bereinige partielle Dateien vor dem harten Abbruch
        PDFGenerator.cleanup(report_id)
        raise

Aufgaben aufrufen

from datetime import timedelta
from django.utils import timezone

# Feuer vergessen (asynchron)
send_welcome_email.delay(user.pk)

# In der Zukunft planen
send_reminder.apply_async(args=[user.pk], countdown=3600)  # 1 Stunde ab jetzt
send_reminder.apply_async(args=[user.pk], eta=timezone.now() + timedelta(days=1))

# Mit Warteschlangen-Routing ausführen
sync_contact_to_crm.apply_async(args=[contact.pk], queue='high_priority')

# Synchron ausführen (nur Tests / Debugging)
result = generate_pdf_report.apply(args=[report.pk])

Beat-Planung (Periodische Aufgaben)

Code-definierter Zeitplan

# config/settings/base.py
from celery.schedules import crontab

CELERY_BEAT_SCHEDULE = {
    'cleanup-expired-sessions': {
        'task': 'users.cleanup_expired_sessions',
        'schedule': crontab(hour=2, minute=0),   # 2 Uhr täglich
    },
    'sync-inventory': {
        'task': 'products.sync_inventory',
        'schedule': 60.0,                         # alle 60 Sekunden
    },
    'weekly-digest': {
        'task': 'notifications.send_weekly_digest',
        'schedule': crontab(day_of_week='monday', hour=8, minute=0),
    },
}

Datenbank-definierter Zeitplan (via django-celery-beat)

# Periodische Aufgaben aus dem Django-Admin oder Code verwalten
from django_celery_beat.models import PeriodicTask, CrontabSchedule
import json

schedule, _ = CrontabSchedule.objects.get_or_create(
    hour='*/6', minute='0',
    timezone='UTC',
)

PeriodicTask.objects.update_or_create(
    name='Inventar alle 6 Stunden synchronisieren',
    defaults={
        'crontab': schedule,
        'task': 'products.sync_inventory',
        'args': json.dumps([]),
        'enabled': True,
    }
)

Canvas: Verketten und Gruppieren von Aufgaben

from celery import chain, group, chord

# Kette: Aufgaben nacheinander ausführen, Ergebnisse übergeben
pipeline = chain(
    fetch_data.s(source_id),
    transform_data.s(),          # empfängt fetch_data-Ergebnis als erstes Argument
    load_to_warehouse.s(),
)
pipeline.delay()

# Gruppe: Aufgaben parallel ausführen
parallel = group(
    send_welcome_email.s(user_id)
    for user_id in new_user_ids
)
parallel.delay()

# Chord: Parallele Aufgaben + Rückruf, wenn alle abgeschlossen
result = chord(
    group(process_chunk.s(chunk) for chunk in data_chunks),
    aggregate_results.s(),       # wird mit Liste der Chunk-Ergebnisse aufgerufen
)
result.delay()

Fehlerbehandlung und Dead-Letter-Queue

# apps/core/tasks.py
from celery.signals import task_failure

@task_failure.connect
def on_task_failure(sender, task_id, exception, args, kwargs, traceback, einfo, **kw):
    """Protokolliere alle Aufgabenfehler in Sentry / Alerting."""
    import sentry_sdk
    with sentry_sdk.new_scope() as scope:
        scope.set_context('celery', {
            'task': sender.name,
            'task_id': task_id,
            'args': args,
            'kwargs': kwargs,
        })
        sentry_sdk.capture_exception(exception)
# Fehlgeschlagene Aufgaben nach maximalen Wiederholungen in die Dead-Letter-Queue leiten
@shared_task(
    bind=True,
    max_retries=3,
    name='payments.charge_card',
)
def charge_card(self, order_id: int) -> None:
    from apps.payments.models import Order, FailedCharge

    try:
        _do_charge(order_id)
    except Exception as exc:
        if self.request.retries >= self.max_retries:
            # In Dead-Letter-Tabelle für manuelle Überprüfung speichern
            FailedCharge.objects.create(
                order_id=order_id,
                error=str(exc),
                task_id=self.request.id,
            )
            return  # Nicht auslösen – Aufgabe ist dauerhaft fehlgeschlagen
        raise self.retry(exc=exc)

Testen von Celery-Aufgaben

Unit-Tests (Kein Broker)

# tests/test_tasks.py
import pytest
from unittest.mock import patch, MagicMock
from apps.notifications.tasks import send_welcome_email

class TestSendWelcomeEmail:

    @pytest.mark.django_db
    def test_sends_email_to_existing_user(self, user):
        with patch('apps.notifications.services.EmailService') as mock_email:
            send_welcome_email(user.pk)
            mock_email.send_welcome.assert_called_once_with(user)

    @pytest.mark.django_db
    def test_skips_missing_user_gracefully(self):
        """Sollte nicht auslösen, wenn der Benutzer zwischen der Einreihung und der Ausführung gelöscht wird."""
        send_welcome_email(99999)  # Nicht vorhandener Benutzer – darf nicht auslösen

Integrationstests mit CELERY_TASK_ALWAYS_EAGER

# config/settings/test.py
CELERY_TASK_ALWAYS_EAGER = True      # Aufgaben in Tests synchron ausführen
CELERY_TASK_EAGER_PROPAGATES = True  # Ausnahmen von Aufgaben erneut auslösen

# tests/test_integration.py
@pytest.mark.django_db
def test_registration_triggers_welcome_email(client):
    with patch('apps.notifications.services.EmailService') as mock_email:
        response = client.post('/api/users/', {
            'email': '[email protected]',
            'password': 'strongpass123',
        })

    assert response.status_code == 201
    mock_email.send_welcome.assert_called_once()

Testen von Wiederholungen

@pytest.mark.django_db
def test_task_retries_on_connection_error():
    with patch('apps.crm.services.CRMClient.sync') as mock_sync:
        mock_sync.side_effect = ConnectionError('timeout')

        with pytest.raises(ConnectionError):
            sync_contact_to_crm.apply(args=[1], throw=True)

        assert mock_sync.call_count == 1  # Nur erster Versuch bei eager

Überwachung

# Aktive Worker und Warteschlangen inspizieren
celery -A config inspect active
celery -A config inspect stats
celery -A config inspect reserved

# Warteschlangenlängen prüfen (Redis)
redis-cli llen celery

# Flower: webbasierter Echtzeit-Monitor
pip install flower
celery -A config flower --port=5555

Anti-Patterns

# SCHLECHT: Modellinstanzen übergeben – sie könnten zum Ausführungszeitpunkt veraltet sein
send_welcome_email.delay(user)        # Niemals ORM-Objekte übergeben
send_welcome_email.delay(user.pk)     # Immer Primärschlüssel übergeben

# SCHLECHT: Aufgaben synchron in Produktions-Views aufrufen
result = generate_report.apply()      # Blockiert den Request-Thread

# SCHLECHT: Nicht-idempotente Aufgabe ohne Sicherungen
@shared_task
def charge_and_fulfill(order_id):
    order.charge()     # Kann doppelt belasten, wenn die Aufgabe wiederholt wird!
    order.fulfill()

# GUT: Idempotent mit Status-Sicherung
@shared_task
def charge_and_fulfill(order_id):
    order = Order.objects.select_for_update().get(pk=order_id)
    if order.status != Order.Status.PENDING:
        return  # Bereits verarbeitet
    order.charge()
    order.fulfill()

Produktions-Checkliste

PrüfungEinstellung
Worker-Neustart bei Absturzsupervisord oder systemd-Einheit
CELERY_TASK_ACKS_LATE = TrueAufgaben bei Worker-Absturz erneut einreihen
CELERY_WORKER_PREFETCH_MULTIPLIER = 1Faire Verteilung langer Aufgaben
Separate Warteschlangen pro Priorität-Q default,high_priority,low_priority
CELERY_TASK_SOFT_TIME_LIMIT gesetztAnmutige Zeitüberschreitung vor hartem Abbruch
Sentry-IntegrationAlle task_failure-Signale erfassen
Flower oder anderer MonitorEinblick in Warteschlangentiefen
Beat läuft nur auf einem KnotenVerhindert doppelte Ausführung geplanter Aufgaben

Verwandte Skills

  • django-patterns — ORM, Service-Schicht und Projektstruktur
  • django-tdd — Testen von Django-Modellen, Views und Services
  • python-testing — pytest-Konfiguration und Fixtures

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

Verwandte Skills