How to Schedule Periodic Tasks with Celery in Django
To schedule periodic tasks in Django with
Celery, use Celery Beat which acts as a scheduler. Define your tasks in Celery, then configure CELERY_BEAT_SCHEDULE in your Django settings to specify the task and its interval.Syntax
Use CELERY_BEAT_SCHEDULE in Django settings to define periodic tasks. Each task needs a unique name, the task path, and a schedule using crontab or timedelta.
- task: The full Python path to the Celery task function.
- schedule: How often to run the task (e.g., every 10 seconds, daily at 7am).
- args: Optional tuple of arguments to pass to the task.
python
CELERY_BEAT_SCHEDULE = {
'task-name': {
'task': 'app.tasks.task_function',
'schedule': crontab(minute='*/10'), # every 10 minutes
'args': (arg1, arg2),
},
}Example
This example shows how to create a simple periodic task that prints a message every minute using Celery and Celery Beat in Django.
python
from celery import Celery from celery.schedules import crontab app = Celery('proj') app.conf.broker_url = 'redis://localhost:6379/0' @app.task def print_hello(): print('Hello from periodic task!') app.conf.beat_schedule = { 'print-every-minute': { 'task': 'proj.print_hello', 'schedule': crontab(minute='*'), }, } if __name__ == '__main__': app.start()
Output
Hello from periodic task! # printed every minute in worker logs
Common Pitfalls
- Not running
celery beatalongside the worker will prevent periodic tasks from running. - Incorrect task path in
CELERY_BEAT_SCHEDULEcauses task not found errors. - Forgetting to configure a message broker like Redis or RabbitMQ stops Celery from working.
- Using
printin tasks may not show output in your console; check worker logs instead.
bash
## Wrong: Missing beat scheduler # Running only celery worker celery -A proj worker --loglevel=info ## Right: Run beat and worker celery -A proj beat --loglevel=info & celery -A proj worker --loglevel=info
Quick Reference
| Setting | Description | Example |
|---|---|---|
| CELERY_BEAT_SCHEDULE | Defines periodic tasks and their schedules | {'task-name': {'task': 'app.tasks.func', 'schedule': crontab(minute='*/5')}} |
| crontab | Schedule using cron syntax | crontab(minute='0', hour='7') # daily at 7am |
| timedelta | Schedule using time intervals | timedelta(seconds=30) # every 30 seconds |
| celery beat | Scheduler process to run periodic tasks | celery -A proj beat --loglevel=info |
| celery worker | Worker process to execute tasks | celery -A proj worker --loglevel=info |
Key Takeaways
Use Celery Beat to schedule periodic tasks in Django with Celery.
Define tasks and their schedules in CELERY_BEAT_SCHEDULE in Django settings.
Always run both celery worker and celery beat processes to enable periodic tasks.
Use crontab or timedelta to specify task intervals clearly.
Check worker logs for task output instead of relying on print statements.