Why do we use scheduled tasks
For the static homepage, considering that the data of the page may be maintained by multiple operating personnel and changes frequently, it is made into a scheduled task, that is, the static is performed regularly
Performing scheduled tasks in Django can be achieved through the django-crontab extension.
pip install django-crontab
Configure installed applications
Add django_crontab to INSTALLED_APPS in settings.py
INSTALLED_APPS = [ ... 'django_crontab', ... ]
Set up the task schedule
Set the scheduled execution time in the configuration file.
Each scheduled task is divided into three parts:
- Task time
Basic format: * * * * * Time sharing day month week order M: minute (0-59). Expressed by * or */1 every minute H: Hour (0-23). (0 means 0 points) D: Day (1-31). m: month (1-12). d: days within a week (0~6, 0 is Sunday).
- Task method
- Task log
The homepage's scheduled tasks are set as follows
# Scheduled tasks CRONJOBS = [ # Execute every 5 minutes to generate homepage static files ('*/5 * * * *','contents.crons.generate_static_index_html','>> /var/log/******/logs/crontab.log') # Here is your log path ]
Start the scheduled task
Add scheduled tasks to the system
python manage.py crontab add
Show activated scheduled tasks
python manage.py crontab show
Remove scheduled tasks
python manage.py crontab remove