0
0
Flaskframework~20 mins

Periodic tasks with Celery Beat in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Celery Beat Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a periodic task is scheduled with Celery Beat?

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?

Flask
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'
AThe send_report task runs only if manually called in the Flask app code.
BThe send_report task runs only once when the app starts.
CThe send_report task runs every second due to the crontab schedule.
DThe send_report task runs automatically every minute without manual triggering.
Attempts:
2 left
💡 Hint

Think about what Celery Beat does with the schedule configuration.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Celery Beat schedule configuration

Which option shows the correct way to define a periodic task schedule in Celery Beat?

Flask
app.conf.beat_schedule = {
    'task-name': {
        'task': 'tasks.example_task',
        'schedule': crontab(hour=0, minute=0)
    }
}
Aapp.conf.beat_schedule = {'task-name': {'task': 'tasks.example_task', 'schedule': crontab(hour=0, minute=0)}}
Bapp.conf.beat_schedule = {'task-name': {'task': 'tasks.example_task', 'schedule': crontab(hour=0, minute=0),}}
Capp.conf.beat_schedule = {'task-name': {'task': 'tasks.example_task', 'schedule': crontab(hour=0 minute=0)}}
Dapp.conf.beat_schedule = {'task-name': {'task': 'tasks.example_task', 'schedule': crontab(hour=0; minute=0)}}
Attempts:
2 left
💡 Hint

Check for missing commas or invalid separators in the dictionary.

🔧 Debug
advanced
2:00remaining
Why does the periodic task not run even though Celery Beat is configured?

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?

Flask
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'
AThe Celery Beat scheduler process is not running alongside the worker.
BThe task name 'tasks.cleanup' is incorrect and should be 'cleanup'.
CThe schedule value 60.0 is invalid and must be a crontab object.
DThe broker URL is missing the password for Redis.
Attempts:
2 left
💡 Hint

Think about what components must run for periodic tasks to execute.

state_output
advanced
2:00remaining
What is the output of this periodic task with state update?

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?

Flask
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'
ACount is 1 printed three times; state['count'] remains 1 due to task isolation.
BCount is 3 printed once; state['count'] resets after each run.
CCount is 0 printed three times; state['count'] never increments.
DCount is 3 printed three times; state['count'] is 3 after 3 runs.
Attempts:
2 left
💡 Hint

Consider how Celery tasks run in separate worker processes.

🧠 Conceptual
expert
3:00remaining
How to ensure a periodic task runs exactly once in a distributed Celery worker setup?

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?

ARun multiple Celery Beat schedulers to balance the load.
BConfigure each worker with a unique queue and schedule the task on all queues.
CUse a distributed lock or singleton pattern to prevent multiple executions.
DSet the task's schedule to crontab with seconds precision.
Attempts:
2 left
💡 Hint

Think about how to avoid race conditions in distributed systems.