0Pricing
Django Academy · บทเรียน

การตั้งค่า Celery ด้วยโบรกเกอร์

เชื่อมต่อ Celery กับ Redis หรือ RabbitMQ

การตั้งค่า Celery ด้วยโบรกเกอร์ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. ✅

คำถามที่พบบ่อย

บทเรียน “การตั้งค่า Celery ด้วยโบรกเกอร์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตั้งค่า Celery ด้วยโบรกเกอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตั้งค่า Celery ด้วยโบรกเกอร์”

เชื่อมต่อ Celery กับ Redis หรือ RabbitMQ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การตั้งค่า Celery ด้วยโบรกเกอร์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุผลที่ต้องใช้ทาสก์เบื้องหลัง
  2. การตั้งค่า Celery ด้วยโบรกเกอร์
  3. การเขียนและเรียกใช้ @shared_task
  4. งานตามกำหนดเวลาด้วย Celery Beat
← กลับไปที่ Django Academy