installing django celery beat pip
- Install the Celery library:
pip install celery
- Install the Django Celery Beat library:
pip install django-celery-beat
- Add 'django_celery_beat' and 'celery' to the INSTALLED_APPS list in your Django project's settings:
INSTALLED_APPS = [
# ... other apps ...
'django_celery_beat',
'celery',
]
- Set up the Celery configuration in your Django project. Create a
celery.py
file in the same directory as yoursettings.py
:
# celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
# create a Celery instance and configure it using the settings from Django
app = Celery('your_project')
# Load task modules from all registered Django app configs.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Auto-discover tasks in all installed apps
app.autodiscover_tasks()
Replace 'your_project' with the actual name of your Django project.
- Configure Django Celery Beat in your project's settings:
# settings.py
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
Replace the URL with the appropriate broker and result backend for your project.
- Run migrations to create necessary database tables for Celery Beat:
python manage.py migrate
- Start Celery Beat:
celery -A your_project beat -l info
Replace 'your_project' with the actual name of your Django project.
- Start Celery worker:
celery -A your_project worker -l info
Replace 'your_project' with the actual name of your Django project.
Now, Django Celery Beat is installed and configured in your Django project. You can use it to schedule periodic tasks.