Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use django-crontab to schedule tasks in django

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use django-crontab timing tasks in django. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

SpringBoot integrates MybatisPlus!

Pip install django-crontab

two。 Add configuration to settings.py INSTALLED_APPS

INSTALLED_APPS = ('django_crontab',...)

3. Write timing functions:

Scheduled tasks can be divided into two types, one is to execute custom mange.py commands, and the other is to execute custom functions.

Create a new myapp/cron.py file in the app of django and put the code that needs to be executed regularly

Example:

Def my_scheduled_job (): pass

4. Add CRONJOBS configuration to settings.py

CRONJOBS = [('* / 5 *', 'myapp.cron.my_scheduled_job')]

You can also define some keyword parameters in two formats:

Format 1:

Requirements: the usual format of cron timing (see Wikipedia and crontab.guru for more examples)

Requirements: scheduled tasks to be executed under the path of python module

Optional: suffixes specific to scheduled tasks (for example, redirect out/err to a file, default is'')

Example:

CRONJOBS = [('* / 1 *', 'appname.test_crontab.test',' > > / home/python/test_crontab.log')]

Note: > > indicates additional write, > indicates overwrite.

Format 2:

Requirement: usual format of cron timing

Requirements: scheduled tasks to be executed under the path of python module

Optional: list of location parameters for the method (default: [])

Optional: dict of the keyword parameter of the method (default: {})

Optional: suffixes specific to scheduled tasks (for example, redirect out/err to a file, default is'')

Example:

CRONJOBS = [('* / 5 *', 'myapp.cron.other_scheduled_job', [' arg1', 'arg2'], {' verbose': 0}), ('0 4 *', 'django.core.management.call_command', [' clearsessions']),]

Students who are familiar with the timing task crontab in Linux may be kind to the syntax of the first parameter above. The above indicates that the code is executed every 1 minute.

The syntax for the scheduled task crontab in Linux is as follows:

* * command minutes (0-59) hours (0-23) which day of the month (1-31) month (1-12) week (0-6) shell script or command

There are several special symbols:

* represents all the numbers in the range of values / represents the meaning of each. * / 5 means every 5 units-from a number to a number, separated by several discrete numbers.

Example:

Every two hours 0 * / 2 * every two hours between 11:00 and 8 a.m., 8: 23 a.m. to 7: 00 a.m. 8 * the 4th of every month and 11: 00 a.m. from Monday to Wednesday * 1-31 1: 00 a.m. 4: 11 a.m.

0 6 * commands > > / tmp/test.log # executes at 6 am every morning, and appends the information to test.log 0 * / 2 * commands # every 2 hours

Interested partners can take an in-depth study of Linux's crontab scheduled tasks. If you don't know and are not familiar with it, you can read it: cron grammar format learning.

5. Add and start scheduled tasks

# add and start scheduled task python manage.py crontab add

Other commands:

# display current scheduled tasks python manage.py crontab show# delete all scheduled tasks python manage.py crontab remove some problems

If configured like this:

BASE_DIR = os.path.dirname (os.path.dirname (os.path.abspath (_ _ file__) CRONJOBS = [('07 * * 1-5mm, 'api.cron.email_to_late_docs',' > > {} '.format (BASE_DIR +' / logs/log_ {:% d_%m_%Y} .log '.format (time.now (), (' 07 * * 1-5mm, 'api.cron.email_ten_days_before' '> {}' .format (BASE_DIR + 'logs/log_ {:% d_%m_%Y} .log' .format (time.now ()] this is where you share scheduled tasks about how to use django-crontab in django I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report