In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the implementation methods of Django timing tasks". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Preface
In order to make some reports, recently, we need to capture competitors' Baidu traffic and mobile traffic from Ai site every day. From the technology I can achieve, there are roughly three forms of implementation:
Regular grasping of locomotive
Python+crontab is fetched regularly and saved in txt or cav or database.
Django timed tasks, displayed by admin system or template system.
The first kind of unfamiliar, but also explore; the second kind of data display also need to write their own, if you use the database, but also use a very primitive connection, at least after using the django; the third advantage is that you can use the admin system or template system, the disadvantage is that the timing task is not familiar. But considering that I intend to learn more about django, I decided to implement it in a third way.
2. Django scheduled tasks
There are basically three ways to implement the scheduled task of Django:
Django-celery
Command+crontab of Django
Some third-party libraries such as django-crontab
First of all, after reading the official tutorials of django-celery, the celery is still quite large, and the tutorials are relatively complex. I skipped the impatient ones first.
Secondly, I read the official document of Django about command and implemented the form of command+crontab. But because it is deployed under virtualenv, it has been unable to achieve. Even though crontab has been set to enter virtualenv and then run command, it still fails (the syntax in crontab is source / home/../bin/activate & & python / home/.../manage.py aizhan_visits).
Finally, the third-party library django-crontab is selected, and the scheduled task of Django under virtualenv is realized unexpectedly.
3. Django-crontab implementation of Django in virtualenv scheduled task 3.1 django-crontab installation
Django-crontab installation: pip install django-crontab
Django-crontab join: just add django-crontab to settings.py 's INSTALLED_APPS. The code is as follows:
INSTALLED_APPS = ('django-crontab',...) 3.2 django-crontab configuration
Django-crontab can run custom commands and functions on a regular basis, because custom command has been implemented when you tried to use command+crontab before, so it is natural to use custom commands.
3.2.1 django-crontab runs commands on a regular basis
I first customized a command aizhan_5domain_visits with reference to the Django official documentation, which is specifically used to capture the traffic of Aishu, and save the results in the sqlite3 database (the specific steps will not be detailed in this article).
Second, I added the command of django-crontab to settings.py:
CRONJOBS = [('47 11 *', 'django.core.management.call_command', [' aizhan_5domain_visits']),]
It means to run the aizhan_5domain_visits command at 11:47 every day. There is only one last step left to load the task.
3.2.2 django-crontab timed run function
Django-crontab can also run functions on a regular basis, but there is a difference in CRONJOBS configuration. CRONJOBS's configuration of the function is as follows:
Analysis results:
The primary mode is very intuitive, which means that the my_scheduled_job program is executed every five minutes.
Intermediate mode has a suffix, which means to output the results of the program my_scheduled_job to a file / tmp/last_scheduled_job.log
Advanced mode adds parameters, where [dumpdata', 'auth'] and {' indent': 4} are parameters, except that the parameters in [] are substituted sequentially, while the parameters in {} specify the variable name, and the last one is also the suffix of the output.
3.3 django-crontab task loading
The django-crontab task is relatively easy to load, as long as you run python manage.py crontab add. If you run crontab-e, you can see that there is an extra line in crontab:
* / home/aizhan/bin/python / home/aizhan/aizhan/manage.py crontab run c27d1050fb7f87225bcff587ef5a35a3 # django-cronjobs for aizhan
This is automatically generated by django-crontab.
If you want to remove all tasks, run python manage.py crontab remove
When you modify the task, you need to run python manage.py crontab add again.
This is the end of the content of "what are the implementation methods of Django timing tasks". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.