Consider a Flask app using Celery with Celery Beat to schedule a task every minute. What is the expected behavior when the scheduler triggers the task?
from celery import Celery from celery.schedules import crontab app = Celery('tasks', broker='redis://localhost:6379/0') @app.task def send_report(): print('Report sent') app.conf.beat_schedule = { 'send-report-every-minute': { 'task': 'tasks.send_report', 'schedule': crontab(minute='*'), }, } app.conf.timezone = 'UTC'
Think about what Celery Beat does with the schedule configuration.
Celery Beat acts as a scheduler that triggers tasks at specified intervals automatically. Here, the task is set to run every minute, so it runs without manual calls.
Which option shows the correct way to define a periodic task schedule in Celery Beat?
app.conf.beat_schedule = {
'task-name': {
'task': 'tasks.example_task',
'schedule': crontab(hour=0, minute=0)
}
}Check for missing commas or invalid separators in the dictionary.
Option B correctly uses a trailing comma after the last item in the dictionary, which is valid in Python. Option B misses a comma between arguments, and D uses a semicolon which is invalid syntax.
A Flask app has Celery and Celery Beat configured with a periodic task. The task is defined and scheduled correctly, but it never runs. What is the most likely cause?
from celery import Celery app = Celery('tasks', broker='redis://localhost:6379/0') @app.task def cleanup(): print('Cleanup done') app.conf.beat_schedule = { 'cleanup-task': { 'task': 'tasks.cleanup', 'schedule': 60.0, }, } app.conf.timezone = 'UTC'
Think about what components must run for periodic tasks to execute.
Celery Beat must be running as a separate scheduler process to send periodic task messages. If it is not running, tasks won't be triggered even if configured.
Given this Celery task that updates a state dictionary each time it runs, what will be the value of state['count'] after 3 executions triggered by Celery Beat?
state = {'count': 0}
@app.task
def increment():
state['count'] += 1
print(f"Count is {state['count']}")
app.conf.beat_schedule = {
'increment-task': {
'task': 'tasks.increment',
'schedule': 10.0,
},
}
app.conf.timezone = 'UTC'Consider how Celery tasks run in separate worker processes.
Each Celery task runs in a separate process, so the state dictionary is not shared or persisted between runs. The count resets to 0 each time, so it prints 1 each run.
You have multiple Celery workers running in parallel with Celery Beat scheduling a periodic task. How do you guarantee the task runs only once per schedule, not multiple times?
Think about how to avoid race conditions in distributed systems.
When multiple workers receive the same scheduled task, a distributed lock ensures only one executes it. Without locking, the task may run multiple times concurrently.