Periodic tasks let your app do things automatically at set times, like sending reminders or cleaning up data. Celery Beat helps schedule these tasks easily.
Periodic tasks with Celery Beat in Django
from celery import Celery from celery.schedules import crontab app = Celery('myapp') app.conf.beat_schedule = { 'task-name': { 'task': 'myapp.tasks.my_task', 'schedule': crontab(minute=0, hour=7), # every day at 7:00 AM 'args': (), }, } @app.task def my_task(): # task code here pass
crontab lets you set schedules like a clock (e.g., every day at 7 AM).
Define your periodic tasks inside beat_schedule with a unique name.
quick_task every 10 seconds using a simple float for seconds.app.conf.beat_schedule = {
'run-every-10-seconds': {
'task': 'myapp.tasks.quick_task',
'schedule': 10.0, # every 10 seconds
'args': (),
},
}send_report once daily at midnight using crontab.app.conf.beat_schedule = {
'daily-report': {
'task': 'myapp.tasks.send_report',
'schedule': crontab(hour=0, minute=0), # every day at midnight
'args': (),
},
}weekly_cleanup every Monday at 9:30 AM.app.conf.beat_schedule = {
'every-monday-morning': {
'task': 'myapp.tasks.weekly_cleanup',
'schedule': crontab(hour=9, minute=30, day_of_week='mon'),
'args': (),
},
}This example sets up a task called say_hello that prints a message every minute. Celery Beat will trigger it automatically.
from celery import Celery from celery.schedules import crontab app = Celery('myapp') app.conf.broker_url = 'redis://localhost:6379/0' app.conf.beat_schedule = { 'say-hello-every-minute': { 'task': 'myapp.tasks.say_hello', 'schedule': crontab(minute='*'), # every minute 'args': (), }, } @app.task def say_hello(): print('Hello from Celery Beat!')
Make sure your Celery worker and Celery Beat scheduler are running to execute periodic tasks.
Use Redis or RabbitMQ as the message broker for Celery to work smoothly.
Test your tasks manually before scheduling to avoid silent failures.
Celery Beat schedules tasks to run automatically at set times.
Use beat_schedule with crontab or seconds to set timing.
Run both Celery worker and Beat to see periodic tasks in action.
Practice
Solution
Step 1: Understand Celery Beat's role
Celery Beat is designed to schedule tasks to run at specific times or intervals automatically.Step 2: Differentiate from other components
Handling HTTP requests, managing migrations, or serving static files are not functions of Celery Beat.Final Answer:
To schedule and run periodic tasks automatically -> Option AQuick Check:
Celery Beat = periodic task scheduler [OK]
- Confusing Celery Beat with Django's request handling
- Thinking Celery Beat manages database or static files
- Assuming Celery Beat runs tasks immediately without schedule
Solution
Step 1: Check crontab syntax
The crontab function requires named arguments like minute and hour with string values representing schedule patterns.Step 2: Validate correct usage
"beat_schedule = { 'task-name': { 'task': 'app.tasks.my_task', 'schedule': crontab(minute='0', hour='*/3') } }" correctly uses crontab(minute='0', hour='*/3') to run every 3 hours at minute 0.Final Answer:
beat_schedule with crontab(minute='0', hour='*/3') -> Option AQuick Check:
Correct crontab syntax = "beat_schedule = { 'task-name': { 'task': 'app.tasks.my_task', 'schedule': crontab(minute='0', hour='*/3') } }" [OK]
- Passing positional arguments instead of named
- Using invalid hour format like '3/0'
- Mixing string and integer types incorrectly
beat_schedule = {
'print-time': {
'task': 'app.tasks.print_time',
'schedule': crontab(minute='*/15')
}
}
What will happen when Celery Beat and worker run?Solution
Step 1: Interpret crontab(minute='*/15')
This means the task runs every 15 minutes, at minute 0, 15, 30, 45 of each hour.Step 2: Understand Celery Beat behavior
When both Beat and worker run, Beat triggers the task on schedule, so it runs repeatedly every 15 minutes.Final Answer:
The task 'print_time' runs every 15 minutes -> Option DQuick Check:
crontab '*/15' means every 15 minutes [OK]
- Thinking it runs only once
- Confusing minute '*/15' with fixed minute 15
- Assuming syntax error without checking carefully
beat_schedule = {
'cleanup-task': {
'task': 'app.tasks.cleanup',
'schedule': crontab(minute=0, hour='*')
}
}
But the task never runs. What is the most likely cause?Solution
Step 1: Check if Celery Beat is running
Celery Beat must be running to send scheduled tasks to the worker.Step 2: Validate other options
Task names can be any string, crontab accepts integer or string for minute, and workers do not need hourly restart.Final Answer:
You forgot to start the Celery Beat service -> Option BQuick Check:
Beat service must run for schedules to trigger [OK]
- Assuming task name format causes failure
- Thinking crontab minute must be string only
- Restarting worker unnecessarily
beat_schedule to achieve this?Solution
Step 1: Define daily task schedule correctly
Use crontab(hour=2, minute=30) or with strings '2' and '30' to run at 2:30 AM daily.Step 2: Define frequent task schedule correctly
Use crontab(minute='*/10') to run every 10 minutes; minute=10 runs only at minute 10 each hour.Step 3: Check all options
{ 'daily-task': { 'task': 'app.tasks.daily', 'schedule': crontab(hour=2, minute=30) }, 'frequent-task': { 'task': 'app.tasks.frequent', 'schedule': crontab(minute='*/10') } } uses correct crontab syntax for both tasks; others have invalid formats or misunderstandings.Final Answer:
Use crontab(hour=2, minute=30) and crontab(minute='*/10') -> Option CQuick Check:
Daily at 2:30 and every 10 min = { 'daily-task': { 'task': 'app.tasks.daily', 'schedule': crontab(hour=2, minute=30) }, 'frequent-task': { 'task': 'app.tasks.frequent', 'schedule': crontab(minute='*/10') } } [OK]
- Using '2:30' as hour value
- Setting minute=10 instead of '*/10' for intervals
- Mixing string and integer types incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
