0
0
Djangoframework~10 mins

Periodic tasks with Celery Beat in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Periodic tasks with Celery Beat
Define Celery Task
Configure Celery Beat Schedule
Start Celery Worker
Start Celery Beat Scheduler
Scheduler triggers task at intervals
Worker executes task
Task completes and waits for next trigger
This flow shows how a periodic task is defined, scheduled, and executed repeatedly by Celery Beat and the Celery worker.
Execution Sample
Django
from celery import shared_task

@shared_task
def say_hello():
    print('Hello from Celery Beat!')
Defines a simple Celery task that prints a message when run.
Execution Table
StepActionScheduler TimeTask TriggeredWorker Output
1Celery Beat scheduler starts00:00NoNo output
2Scheduler checks schedule00:05NoNo output
3Scheduler time matches task interval00:10YesTask queued
4Worker picks up task00:10YesPrints: Hello from Celery Beat!
5Task completes00:10YesIdle until next trigger
6Scheduler waits for next interval00:15NoNo output
7Scheduler time matches task interval again00:20YesTask queued
8Worker executes task again00:20YesPrints: Hello from Celery Beat!
9Task completes00:20YesIdle until next trigger
💡 The scheduler runs continuously; this trace shows two task executions at 10 and 20 seconds.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7After Step 8Final
Scheduler Time00:0000:1000:1000:2000:2000:20
Task TriggeredNoYesYesYesYesYes
Worker OutputNo outputTask queuedPrints: Hello from Celery Beat!Task queuedPrints: Hello from Celery Beat!Idle until next trigger
Key Moments - 3 Insights
Why doesn't the task run immediately when the scheduler starts?
The scheduler waits until the first scheduled interval matches the current time before triggering the task, as shown in steps 1 and 2 where no task is triggered.
How does the worker know when to execute the task?
The scheduler queues the task at the scheduled time (step 3), and the worker continuously checks the queue to pick up and execute tasks (step 4).
Does the task run only once or repeatedly?
The task runs repeatedly at each scheduled interval, demonstrated by the repeated triggers and executions at steps 3-5 and 7-9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the worker first print the message?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Check the 'Worker Output' column to see when the message is printed.
At which scheduler time does the task get triggered again after the first execution?
A00:15
B00:20
C00:10
D00:05
💡 Hint
Look at the 'Scheduler Time' and 'Task Triggered' columns for repeated triggers.
If the task interval was changed to 15 seconds, which step would change in the execution table?
AStep 3
BStep 7
CStep 6
DStep 9
💡 Hint
Changing the interval affects when the scheduler waits and triggers tasks, check the 'Scheduler waits' step.
Concept Snapshot
Periodic tasks with Celery Beat:
- Define tasks with @shared_task decorator.
- Configure schedule in Celery Beat settings.
- Run Celery worker and Beat scheduler.
- Beat triggers tasks at intervals.
- Worker executes tasks repeatedly.
- Useful for regular background jobs.
Full Transcript
This visual trace shows how periodic tasks work with Celery Beat in Django. First, you define a task using the @shared_task decorator. Then, you configure the schedule so Celery Beat knows when to run it. When you start the Celery worker and the Beat scheduler, the scheduler waits until the scheduled time to trigger the task. The worker picks up the task from the queue and runs it, printing the message. This process repeats at each interval, showing how Celery Beat automates periodic background jobs.