Skills Development Django Celery Async Task Management

Django Celery Async Task Management

v20260704
django-celery
A powerful solution for handling background jobs and asynchronous tasks within Django applications using Celery. It facilitates task queuing, reliable worker management, scheduled execution (cron jobs), and robust error handling. Ideal for sending emails, processing heavy data loads, or managing complex, multi-step workflows without blocking the main thread.
Get Skill
334 downloads
Overview

Django + Celery 非同期タスク

Django でのバックグラウンドジョブと非同期処理。

使用時期

  • メール送信をバックグラウンドで実行
  • 重い処理をスケジュール
  • 定期的なタスクを実行(日報、クリーンアップ)
  • 外部API呼び出しをキューイング
  • 複雑なワークフローを調整

セットアップ

1. Celery インストール

pip install celery redis

2. タスク定義

from celery import shared_task

@shared_task
def send_email(recipient):
    # メール送信ロジック
    pass

3. ワーカー起動

celery -A myapp worker -l info

タスク

非同期実行

send_email.delay(recipient)  # すぐにキューに追加、非同期実行

スケジューリング

from celery.schedules import crontab

app.conf.beat_schedule = {
    'send-report-daily': {
        'task': 'app.tasks.send_report',
        'schedule': crontab(hour=9, minute=0),
    },
}

エラーハンドリング

  • リトライロジック実装
  • デッドレター処理
  • ロギング構成
  • モニタリング設定(Flower)

詳細については、ドキュメントを参照してください。

Info
Category Development
Name django-celery
Version v20260704
Size 1.51KB
Updated At 2026-07-09
Language