0
0
DjangoHow-ToBeginner · 4 min read

How to Use Celery Beat in Django for Scheduled Tasks

To use celery beat in Django, first configure Celery with your Django project and add django-celery-beat to your installed apps if you want to use the database scheduler. Then define periodic tasks in the Celery Beat schedule either in your celery.py or Django settings, and run the celery beat service alongside your Celery worker to execute scheduled tasks automatically.
📐

Syntax

Celery Beat uses a schedule dictionary to define periodic tasks. Each task has a name, the task function path, and a schedule interval.

Key parts:

  • task: The full Python path to the Celery task function.
  • schedule: How often to run the task, using crontab or timedelta.
  • args: Optional tuple of arguments to pass to the task.
python
from celery import Celery
from celery.schedules import crontab

app = Celery('proj')

app.conf.beat_schedule = {
    'task-name': {
        'task': 'app.tasks.example_task',
        'schedule': crontab(minute=0, hour='*/1'),  # every hour
        'args': (),
    },
}

app.conf.timezone = 'UTC'
💻

Example

This example shows how to set up Celery Beat in a Django project to run a task every minute.

It includes the Celery app configuration, a sample task, and the beat schedule.

python
# proj/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

app.conf.beat_schedule = {
    'print-every-minute': {
        'task': 'app.tasks.print_hello',
        'schedule': crontab(),  # every minute
    },
}
app.conf.timezone = 'UTC'

# app/tasks.py
from celery import shared_task

@shared_task
def print_hello():
    print('Hello from Celery Beat!')

# Run commands in terminal:
# celery -A proj beat --loglevel=info
# celery -A proj worker --loglevel=info
Output
Hello from Celery Beat! Hello from Celery Beat! Hello from Celery Beat! ... (prints every minute)
⚠️

Common Pitfalls

Common mistakes when using Celery Beat in Django include:

  • Not running the celery beat service alongside the worker, so scheduled tasks never trigger.
  • Forgetting to add django-celery-beat to INSTALLED_APPS when using its database scheduler.
  • Incorrect timezone settings causing tasks to run at unexpected times.
  • Defining tasks but not importing them properly, so Celery cannot find them.
bash
# Wrong: Not running celery beat
# Only running worker:
# celery -A proj worker --loglevel=info

# Right: Run both beat and worker
# In separate terminals:
# celery -A proj beat --loglevel=info
# celery -A proj worker --loglevel=info
📊

Quick Reference

Summary tips for using Celery Beat in Django:

  • Configure Celery and Celery Beat in your Django project settings.
  • Define periodic tasks in beat_schedule using crontab or timedelta.
  • Run celery beat and celery worker together to execute scheduled tasks.
  • Use django-celery-beat for database-backed schedules and admin UI.
  • Check timezone settings to ensure tasks run at correct local times.

Key Takeaways

Always run both celery beat and celery worker processes to enable scheduled tasks.
Define your periodic tasks clearly in the beat_schedule dictionary with proper intervals.
Use django-celery-beat for easier schedule management via Django admin.
Set the timezone correctly in Celery config to avoid timing issues.
Ensure your tasks are properly imported and registered with Celery.