We have heard of schedulers as in google scheduler, but have you ever used schedulers in your web applications to call some specific methods for a fixed time interval. Say a web application needs to send a mail to a user on every Monday at 7:30 p.m., then we use a scheduler to execute a defined method on every 19 hours and 30 minutes of Monday. So today we will discuss such a scheduler in the Django framework commonly known as celery beat.
Celery beat is a python task scheduling module. It performs specified tasks at regular intervals irrespective of any other process/event occurring. Celery is compatible with Django since it provides many predefined methods for executing asynchronously as well as synchronously tasks on schedule as well as periodically. Let's try building a periodic task using celery beat in a Django app. Firstly, we need to build a Django project, demo_project, and then within it a Django app, demo_app. Here is the terminal syntax,
- $ django-admin startproject demo_project
- $ cd demo_project
- $ python manage.py startapp demo_app
Now, we need to install celery beat in our virtual environment by using a simple command,
- $ pip install django-celery-beat
After installation, we will need to configure our
demo_project's settings by adding 2 installed apps
'demo_app' and
'django_celery_beat' in settings.py like this,
- INSTALLED_APPS = [
-
- 'demo_app',
- 'django_celery_beat',
- ]
Celery beat requires to store some data in the database in order to schedule task periodically. Hence we will create the tables for the same by the terminal syntax,
- $ python manage.py migrate
So, we are ready to create our periodic task. We will create a new file in
demo_app named '
tasks.py' and then within it define a task which prints a message for every Thursday at 7:30 a.m to all its users. Here is how the task is defined,
- from __future__ import absolute_import, unicode_literals
- from celery import shared_task
-
- @shared_task
- def test(message):
- print(f"Message: {message}")
Then, we need to configure our
demo_project to perform the test task on every Thursday at 7:30 a.m. This can be done by creating '
celery.py' in
demo_project and set a schedule by specifying the beat_schedule dictionary with the keys 'task', 'schedule' and 'args' as shown below,
- from __future__ import absolute_import, unicode_literals
-
- import os
- from celery import Celery
- from celery.schedules import crontab
-
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo_project.settings')
-
- app = Celery('demo_project')
- app.config_from_object('django.conf:settings', namespace='CELERY')
-
- app.conf.beat_schedule = {
- 'print-every-thrusday': {
- 'task': 'demo_app.tasks.test',
- 'schedule': crontab(hour=7, minute=30, day_of_week=4),
- 'args': ('Its Thrusday!',)
- },
- }
- app.conf.timezone = 'UTC'
- app.autodiscover_tasks()
Crontab schedules help to decide when the task is executed, for example, a particular time of day or day of the week, or even month of the year. Likewise, there are solar schedules that allow you to schedule your task according to sunrise, sunset, dawn, or dusk by providing the latitude and longitude of the place. So that's it, we have scheduled a task in a web application independent of client-server interactions on a periodic interval using celery beat. To learn more about celery, click
here to get to the source.