0
0
Flaskframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Periodic tasks with Celery Beat
Define Celery app
Configure Celery Beat schedule
Start Celery worker and Beat
Beat triggers task periodically
Worker executes task
Task completes and waits for next trigger
This flow shows how Celery Beat schedules tasks periodically and the worker executes them repeatedly.
Execution Sample
Flask
from celery import Celery
app = Celery('tasks', broker='redis://localhost')
app.conf.beat_schedule = {
  'say-hello-every-10-seconds': {
    'task': 'tasks.say_hello', 'schedule': 10.0
  }
}
@app.task
def say_hello():
  print('Hello!')
This code sets up a Celery app with a Beat schedule to run say_hello every 10 seconds.
Execution Table
StepActionBeat Schedule CheckTask TriggeredWorker ExecutesOutput
1Start Celery Beat and WorkerBeat running, waiting 10sNoNo
210 seconds passedBeat triggers say_helloYesYesHello!
3Task completesBeat resets timerNoNo
4Another 10 seconds passedBeat triggers say_helloYesYesHello!
5Task completesBeat resets timerNoNo
💡 This process repeats indefinitely until stopped.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Beat Timer0s10s0s10s0sRepeats
Task TriggeredFalseTrueFalseTrueFalseRepeats
Worker StateIdleRunning say_helloIdleRunning say_helloIdleRepeats
Key Moments - 2 Insights
Why doesn't the task run immediately when Celery Beat starts?
Celery Beat waits for the first schedule interval (10 seconds) before triggering the task, as shown in execution_table step 1 where the timer counts up before triggering.
How does Celery Beat know when to trigger the task again?
After each task execution, Beat resets its timer and waits for the next interval, visible in execution_table steps 3 and 5 where the timer resets to 0s.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the worker doing?
AExecuting the say_hello task
BIdle, waiting for task
CStarting Celery Beat
DStopping the worker
💡 Hint
Check the 'Worker Executes' column at step 2 in execution_table
At which step does the Beat reset its timer after task completion?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Beat Schedule Check' column for timer reset in execution_table
If the schedule was changed to 5 seconds, how would the 'Beat Timer' variable change in variable_tracker?
AIt would count to 10 seconds before resetting
BIt would count to 5 seconds before resetting
CIt would never reset
DIt would reset randomly
💡 Hint
Refer to the 'Beat Timer' row in variable_tracker and relate to schedule timing
Concept Snapshot
Periodic tasks with Celery Beat:
- Define Celery app and tasks
- Configure beat_schedule with task and interval
- Start Celery worker and Beat together
- Beat triggers tasks on schedule
- Worker executes tasks repeatedly
- Process runs continuously until stopped
Full Transcript
This visual execution shows how Celery Beat works with a Flask Celery app to run periodic tasks. First, the Celery app is defined with a Redis broker. Then, the beat_schedule config sets a task to run every 10 seconds. When Celery Beat and the worker start, Beat waits 10 seconds before triggering the task. The worker runs the task, printing 'Hello!'. After the task finishes, Beat resets its timer and waits for the next 10 seconds. This cycle repeats indefinitely. Variables like the Beat timer and worker state change accordingly. Key moments include understanding why the task doesn't run immediately and how Beat resets its timer. The quizzes test understanding of worker actions, timer resets, and schedule changes.