use for run a schedule time automatically
pip django-celery-beat
INSTALLED_APPS = [
...
'django_celery_beat',
]
Migrate Database
python manage.py migrate#settings.py
#Configure Celery Beat Scheduler:
CELERY_BEAT_SCHEDULE = {
'my_task': {
'task': 'myapp.tasks.my_task',
'schedule': 300.0, # Run every 5 minutes (300 seconds)
},
}
# Configure Celery Beat Settings:
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_BEAT_LOG_LEVEL = 'INFO'
To start Celery Beat, you can run the following command:
celery -A myproject worker -l info -B
Example
from celery import shared_task
@shared_task
def test_task():
for i in range(1,10):
print(f"Number: {i}")#settings.py
CELERY_BEAT_SCHEDULE = {
'test_task': {
'task': 'tasks.test_task',
'schedule': 300.0, # Run every 5 minutes (300 seconds)
},
}
we can use schedule in celery.py
#Celery Beat Settings
app.conf.beat_schedule = {
'send-mail-every-day-at-8' : {
'task': 'emailExample.tasks.send_mail_func',
'schedule': crontab(hour = 15, minute = 42),
}
}