What if your app could do important jobs on its own, right when you want it to?
Why Periodic tasks with Celery Beat in Flask? - Purpose & Use Cases
Imagine you have a web app that needs to send reminder emails every day at 9 AM. You try to write code that checks the time and sends emails manually inside your app.
Manually checking time and running tasks is tricky and unreliable. Your app might miss the exact time, or the server could restart and lose track. It's hard to keep tasks running smoothly without errors.
Celery Beat lets you schedule tasks to run automatically at set times. It handles timing and retries for you, so your reminders always go out on time without extra code.
while True: if current_time == '09:00': send_email() sleep(60)
@celery.task def send_email(): # send email code celery.conf.beat_schedule = { 'daily-email': { 'task': 'send_email', 'schedule': crontab(hour=9, minute=0), }, }
You can easily run tasks on a schedule without worrying about timing or server restarts.
A fitness app sends daily workout reminders every morning automatically, helping users stay on track without manual effort.
Manual timing is unreliable and hard to maintain.
Celery Beat automates scheduled tasks with precision.
This makes your app more reliable and easier to manage.
Practice
Solution
Step 1: Understand Celery Beat's role
Celery Beat is a scheduler that triggers tasks periodically without manual intervention.Step 2: Compare with other options
Handling HTTP requests, managing migrations, and serving static files are unrelated to scheduling tasks.Final Answer:
To schedule tasks to run automatically at specific times -> Option AQuick Check:
Celery Beat = Task scheduler [OK]
- Confusing Celery Beat with request handling
- Thinking it manages database or static files
- Assuming it runs tasks only once
Solution
Step 1: Identify task decorator
Periodic tasks in Celery are defined with the @celery.task decorator.Step 2: Check syntax correctness
@celery.task\ndef my_task():\n print('Hello') correctly uses @celery.task and a normal function. @app.route('/task')\ndef my_task():\n print('Hello') uses Flask route decorator, which is unrelated. def my_task():\n print('Hello') lacks any decorator. @celery.task\nasync def my_task():\n print('Hello') uses async which is not standard for Celery tasks.Final Answer:
@celery.task\ndef my_task():\n print('Hello') -> Option BQuick Check:
Use @celery.task to define Celery tasks [OK]
- Using Flask route decorator instead of @celery.task
- Defining tasks without any decorator
- Using async def which Celery does not support by default
beat_schedule = {
'print-every-10-seconds': {
'task': 'app.print_message',
'schedule': 10,
},
}Solution
Step 1: Understand the schedule value
The 'schedule' key with value 10 means the task runs every 10 seconds.Step 2: Confirm task repetition
Celery Beat schedules tasks repeatedly at the interval given, so it runs every 10 seconds, not once or every 10 minutes.Final Answer:
The task 'print_message' runs every 10 seconds -> Option CQuick Check:
Schedule=10 means every 10 seconds [OK]
- Thinking schedule is minutes instead of seconds
- Assuming task runs only once
- Confusing syntax with crontab format
beat_schedule = {
'task1': {
'task': 'app.task1',
'schedule': crontab(minute='*/5')
},
'task2': {
'task': 'app.task2',
'schedule': '10',
}
}Solution
Step 1: Check 'schedule' types
For 'task1', schedule uses crontab correctly. For 'task2', schedule is a string '10', which should be an integer 10 or a crontab object.Step 2: Validate other parts
Task names as strings are correct references. Import errors are not shown here. Dictionary keys can be strings.Final Answer:
'schedule' for 'task2' should be an integer or crontab, not a string -> Option DQuick Check:
Schedule must be int or crontab, not string [OK]
- Using string instead of int for schedule interval
- Confusing task names with function objects
- Assuming dictionary keys must be integers
beat_schedule entry is correct?Solution
Step 1: Understand crontab day_of_week format
Celery's crontab accepts day_of_week as string names like 'mon', 'tue', or integers 0-6 (0=Sunday).Step 2: Evaluate options
{'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='mon')} uses 'mon' string which is correct. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week=2)} uses integer 2 which is Tuesday (0=Sunday). {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='tue')} uses 'tue' string which is Tuesday. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='Monday')} uses 'Monday' full name which is not supported.Final Answer:
{'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='mon')} -> Option AQuick Check:
Use 'mon' string for Monday in crontab [OK]
- Using full day names like 'Monday' instead of 'mon'
- Using string numbers like '1' instead of integers or names
- Confusing day_of_week numbering starting at Sunday
Ready to prove it?
Pass the test and this pattern moves unknown
