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

What is the implementation method of Python timing task

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about the implementation of Python timing tasks, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Scheduled tasks

Scheduled task, as its name implies: a regularly executed task can be a bash command or a script file. It is usually used when we need to do something at a particular time.

For example: perform automatic regression test of full-business scenario interface at 8 o'clock every night, or steal food from your QQ Farm every 20 minutes!

2. Scheduled tasks of Python

If it is a large and medium-sized company, there must be a corresponding platform for everyone to access. Generally speaking, the name or description of such a platform is: distributed task scheduling platform. If not, there will be jenkins to meet our needs.

Remembering that bloggers used to be "young" (when they were at work), the company neither used jenkins nor had such a big platform to access. How do I do scheduled tasks?

Let's look at a piece of code:

Import timefrom datetime import datetimedef func (): # this is the ui/ interface automation method for scheduled tasks to be executed passdef main (): while True: # get the current time now = datetime.now () if now.hour = = 20 and now.minute = = 0: # when the time arrives at 08:00 in the evening We started running automated tasks print ("task execution") func () time.sleep (60) if _ _ name__ = "_ _ main__": main ()

I don't know if anyone still uses this way to perform automated tests on a regular basis, but since it's 2021, let's be more advanced. So what about scheduled tasks in Python? Keep looking.

2.1 several common scenarios 2.1.1 schedule

If you are just the simple request mentioned above, then I suggest using the schedule library directly. I personally feel that this library is an upgraded version of the primary school version, with some extensions to my limited sleep tasks:

Support for periodic execution of tasks

Execution time is more humane to read.

It also supports weekly X execution.

Install scheduler

Pip install schedule

The example given on the official website

Import scheduleimport time# defines an execution method def job (): print ("Isimm working...") # execute jobschedule.every (10) .minutes.do (job) # every hour jobschedule.every () .hour.do (job) # execute jobschedule.every () .day.at ("10:30") .do (job) # execute schedule.every every Monday at 10:30 () .monday.do (job) # executes schedule.every () .wednesday.at ("13:15") .do (job) # every Wednesday at 13:15, executes schedule.every () .minute.at (": 17") .do (job) while True: # enters the schedule loop schedule.run_pending () time.sleep (1)

As you can see, api is very simple and semantically clear. Basically meet our needs ~ if we just want to do something regularly, I think this library is very human.

2.1.2 Jenkins

This tool is better known than everyone. Jenkins is a more powerful software. It can get through git/svn and also supports scheduled tasks. There are rich plug-ins, such as email, which is a complete CI/CD solution. The record of the execution results of the task is also traceable. It is estimated that the only drawback is the introduction of additional systems. Maybe I'm just an automated testing framework, but now I'm going to build a set of jenkins.

If you have code changes-> perform scheduled tasks similar requirements, but also need to improve the access control system, then jenkins will be your best choice.

2.1.3 Celery

I don't know much about this guy, but python+celery seems to be a regular match. I don't know if I understand it correctly:

Celery is a task queue, you can make the rules for the execution of tasks, put them in the queue, and there will be dedicated consumers to help you perform these tasks.

At this point, I have to mention persistence.

2.2 persistence of extra-topic words

What is persistence? In popular terms, instantaneous data (such as data in memory, which cannot be permanently saved) is persisted into persistent data (such as persisted to a database, which can be preserved for a long time).

We can either put the data in a file or put it in a database (actually on disk) for persistent storage.

The purpose of this: the data can be stored and will be available next time. There is too much content in this, so I intend to open another section later.

Our variables in Python, ah, these data are stored in memory, so our schedule just now does not support persistence. (or maybe I didn't study it.)

Does jenkins support it? He is supportive, you think, you set up a project with some job configuration, when to execute, what to do, and finally save.

The above is a persistence process.

After you have saved the data, you can get the task information for the project next time, which means that even if you restart jenkins, the task will still exist.

Indicates that the task itself is kept as a piece of data for a long time.

2.2.1 ApScheduler

"APScheduler is a Python library that allows you to schedule Python code to be executed later, either once or periodically. You can add new jobs or delete old ones depending on you. If you store work in the database, they will also restart the surviving scheduler and keep it in state. When you restart the scheduler, it will run all the jobs it should run when offline 1."

This is an introduction from the official website of ApScheduler, which generally supports the addition, deletion, modification and query (persistence) of tasks, and also supports the execution of scheduled tasks. It is relatively lightweight, not as complex as celery, nor as crude as schedule. Since we are not going to introduce jenkins, generally speaking, ApScheduler is the best choice for us to perform scheduled tasks.

After reading the above, do you have any further understanding of the implementation of Python scheduled tasks? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report