Celery Beat is a scheduler that kicks off tasks at regular intervals, which are then executed by Celery workers. It helps automate periodic tasks like sending emails or cleaning databases.
Click to reveal answer
intermediate
How do you define a periodic task using Celery Beat in Flask?
You define periodic tasks by adding a schedule to Celery Beat's configuration, usually in the 'beat_schedule' dictionary, specifying the task name, the function to call, and the interval or crontab timing.
Click to reveal answer
beginner
What is the role of the 'beat_schedule' setting in Celery Beat?
'beat_schedule' is a dictionary where each key is a task name and the value defines when and how often the task runs, using intervals or crontab expressions.
Click to reveal answer
beginner
Why do you need to run both Celery worker and Celery Beat for periodic tasks?
Celery Beat only schedules tasks but does not execute them. The Celery worker listens for tasks and runs them. Both must run simultaneously for periodic tasks to work.
Click to reveal answer
beginner
How can you test if your periodic task with Celery Beat is working correctly?
You can check the Celery worker logs to see if the task runs at the scheduled times, or add print/log statements inside the task function to confirm execution.
Click to reveal answer
What does Celery Beat do in a Flask app?
ASchedules tasks to run periodically
BExecutes tasks immediately
CStores task results
DManages database connections
✗ Incorrect
Celery Beat schedules tasks to run at set intervals but does not execute them.
Which configuration dictionary holds periodic task schedules in Celery Beat?
Which of these can you use to specify task timing in Celery Beat?
ACSS selectors
BSQL query
CHTML tags
Dcrontab
✗ Incorrect
Crontab expressions define schedules like 'every day at 5pm'.
How can you confirm a periodic task ran successfully?
ARestart the Flask app
BCheck Celery worker logs
CDelete the task
DChange the broker URL
✗ Incorrect
Logs show when tasks are executed by the worker.
Explain how Celery Beat and Celery worker work together to run periodic tasks in a Flask app.
Think about who plans the task and who actually does it.
You got /4 concepts.
Describe how to set up a simple periodic task using Celery Beat in Flask.
Start from writing the task to running the scheduler and worker.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Celery Beat in a Flask application?
easy
A. To schedule tasks to run automatically at specific times
B. To handle HTTP requests asynchronously
C. To manage database migrations
D. To serve static files efficiently
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 A
Quick Check:
Celery Beat = Task scheduler [OK]
Hint: Celery Beat is all about timing tasks automatically [OK]
Common Mistakes:
Confusing Celery Beat with request handling
Thinking it manages database or static files
Assuming it runs tasks only once
2. Which of the following is the correct way to define a periodic task in Celery Beat using Flask?
easy
A. @app.route('/task')\ndef my_task():\n print('Hello')
B. @celery.task\ndef my_task():\n print('Hello')
C. def my_task():\n print('Hello')
D. @celery.task\nasync def my_task():\n print('Hello')
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 B
Quick Check:
Use @celery.task to define Celery tasks [OK]
Hint: Use @celery.task decorator for Celery tasks [OK]
Common Mistakes:
Using Flask route decorator instead of @celery.task
Defining tasks without any decorator
Using async def which Celery does not support by default
3. Given this Celery Beat schedule snippet, what will happen?
A. The dictionary keys 'task1' and 'task2' must be integers
B. 'task' names must be functions, not strings
C. Missing import for crontab causes runtime error
D. 'schedule' for 'task2' should be an integer or crontab, not a string
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 D
Quick Check:
Schedule must be int or crontab, not string [OK]
Hint: Schedule values must be int or crontab, not quoted strings [OK]
Common Mistakes:
Using string instead of int for schedule interval
Confusing task names with function objects
Assuming dictionary keys must be integers
5. You want to run a Flask Celery task every Monday at 8:30 AM using Celery Beat. Which beat_schedule entry is correct?
hard
A. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='mon')}
B. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week=2)}
C. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='tue')}
D. {'task': 'app.weekly_task', 'schedule': crontab(hour=8, minute=30, day_of_week='Monday')}
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 A
Quick Check:
Use 'mon' string for Monday in crontab [OK]
Hint: Use three-letter lowercase day names like 'mon' for crontab [OK]
Common Mistakes:
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.