0Pricing
Django Academy · レッスン

Brokerを使ったCeleryの設定

CeleryをRedisまたはRabbitMQに接続します

「Brokerを使ったCeleryの設定」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What a Broker Is

Celery needs a broker: a middleman that holds queued tasks until a worker grabs them. Think of it as the inbox between your app and the workers.

Redis or RabbitMQ

The two common brokers are Redis (simple, fast, great default) and RabbitMQ (heavier, very robust). For most Django apps, Redis is plenty. 📨

Install the Packages

Start by installing Celery and the Redis client so Celery can talk to your broker over the network.

pip install celery redis

Create the Celery App

Add a celery.py next to settings.py. It creates the Celery instance and tells it where to find your Django config.

import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
app = Celery("myproject")

Read Config from Settings

Point Celery at your Django settings so all its options live in one place, namespaced with a CELERY prefix.

app.config_from_object("django.conf:settings", namespace="CELERY")

Auto-Discover Tasks

One line tells Celery to scan every installed app for a tasks module, so you never have to register tasks by hand.

app.autodiscover_tasks()

Wire It into __init__

In your project __init__.py, import the Celery app so it loads whenever Django starts. This makes the shared task decorator work.

from .celery import app as celery_app
__all__ = ("celery_app",)

Set the Broker URL

In settings.py, point CELERY_BROKER_URL at your running Redis instance so producers and workers share the same queue.

CELERY_BROKER_URL = "redis://localhost:6379/0"

Add a Result Backend

If you want to read task results later, set a result backend. Redis can serve double duty as both broker and result store.

CELERY_RESULT_BACKEND = "redis://localhost:6379/0"

Start a Worker

Run the worker from a terminal. It connects to the broker and waits for jobs. Keep your Django server running in a separate terminal. 🛠️

celery -A myproject worker -l info

Two Processes, Always

Remember: the web server and the worker are separate processes. Both must run for background tasks to actually execute.

Quick Check

What job does the broker do in a Celery setup?

Recap: Celery Setup

Install Celery and a broker like Redis, create celery.py, set the broker URL, and run a worker in its own terminal. You are ready to queue. ✅

よくある質問

「Brokerを使ったCeleryの設定」レッスンは無料ですか?

はい。「Brokerを使ったCeleryの設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。

「Brokerを使ったCeleryの設定」で何を学びますか?

CeleryをRedisまたはRabbitMQに接続します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Django Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Brokerを使ったCeleryの設定」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDjango Academyレッスンでコードを書いて実行できますか?

はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Background Tasksが必要な理由
  2. Brokerを使ったCeleryの設定
  3. @shared_taskの作成と呼び出し
  4. Celery Beatによる定期ジョブ
← Django Academyに戻る