งานตามกำหนดเวลาด้วย Celery Beat
เรียกใช้งานเป็นระยะตามกำหนดเวลา
งานตามกำหนดเวลาด้วย Celery Beat เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond One-Off Tasks
So far tasks run when you queue them. But some jobs should run on a clock: nightly reports, hourly cleanups, weekly digests. That is where Beat comes in. ⏰
What Celery Beat Is
Celery Beat is a scheduler. It is a separate process that wakes up on time and drops scheduled tasks onto the same queue your workers read.
Beat Plus Worker
Beat only queues jobs on schedule; it never runs them itself. You still need a worker running to actually execute what Beat enqueues.
Install the Scheduler
The django-celery-beat package stores your schedule in the database, so you can even edit it from the admin later. Install it first.
pip install django-celery-beatDefine a Beat Schedule
In settings, CELERY_BEAT_SCHEDULE is a dict mapping a name to a task path and how often to run it.
CELERY_BEAT_SCHEDULE = {
"daily-report": {
"task": "reports.tasks.send_report",
"schedule": 86400.0,
},
}Intervals in Seconds
A plain number means "every N seconds". So 86400.0 is once a day and 3600.0 is once an hour. Simple, but a bit rigid for real calendars.
Crontab for Real Calendars
For "8am every Monday" use a crontab schedule. It reads like a cron line and handles days, hours, and minutes precisely.
from celery.schedules import crontab
schedule = crontab(hour=8, minute=0, day_of_week=1)Wire Crontab In
Drop a crontab object straight into the schedule value to control exactly when a task fires.
"weekly-digest": {
"task": "news.tasks.digest",
"schedule": crontab(hour=8, minute=0, day_of_week="mon"),
}Run the Beat Process
Start Beat in its own terminal, beside the worker. Now you have three processes: web server, worker, and the beat scheduler.
celery -A myproject beat -l infoAvoid Duplicate Beats
Run only one Beat process. Two schedulers means every job fires twice, which can double-send emails or double-bill users. 🚫
Edit Schedules from Admin
With django-celery-beat, schedules live in the database, so non-developers can add or pause periodic tasks right from the Django admin.
Quick Check
What is the relationship between Celery Beat and the worker?
Recap: Scheduled Jobs
Celery Beat queues tasks on a schedule using intervals or crontab. Run exactly one Beat plus a worker, and edit schedules from the admin. 🗓️
คำถามที่พบบ่อย
บทเรียน “งานตามกำหนดเวลาด้วย Celery Beat” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “งานตามกำหนดเวลาด้วย Celery Beat” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “งานตามกำหนดเวลาด้วย Celery Beat”
เรียกใช้งานเป็นระยะตามกำหนดเวลา คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “งานตามกำหนดเวลาด้วย Celery Beat” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุผลที่ต้องใช้ทาสก์เบื้องหลัง
- การตั้งค่า Celery ด้วยโบรกเกอร์
- การเขียนและเรียกใช้ @shared_task
- งานตามกำหนดเวลาด้วย Celery Beat