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 Python to realize timing Program

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Python to achieve timing program", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to use Python to achieve timing programs"!

Timer concept

What is a timer? It means that starting from a specified time, after a specified time, and then triggering an event, the user can customize the period and frequency of the timer.

Implement a simple timing program

Option one

In Python, how do you define a timer function? Let's look at the first method first. Suppose we need to execute a function userCountFunc that needs to be executed every other hour. So, we can write like this:

Def main (): startCronTask (userCountFunc, minutes=60) if _ _ name__ = "_ _ main__": main ()

As in the above code, after defining a main function, we define a timing function startCronTask. The first parameter is the name of the function, the second parameter is time, and the second parameter indicates how long it will take to call the function of the next parameter. The first parameter is the function object, which is passed by the function name (such as userCountFunc), not the function execution statement userCountFunc (), otherwise an error will be reported. Then, when implementing this function, you need to introduce the timing function, and there is a timing task module BlockingScheduler in Python:

From apscheduler.schedulers.blocking import BlockingSchedulerdef startCronTask (task, * config): # BlockingScheduler scheduler = BlockingScheduler () scheduler.add_job (task, "interval", * * config) scheduler.start ()

After defining a scheduling module, the actual timing scheduling function is completed. Next, you need to implement the scheduled logic function userCountFunc:

Def userCountFunc (): logger.info ("count user")...

In this way, for scheme 1, the simple timing function is completed.

Option 2

Solution 1 introduces the BlockingScheduler module that comes with Python. Besides BlockingScheduler, you can also implement timer timer through threads in Python to simply take a look at the code:

Import threadingdef timerFunc (): print ("Hello World~") timer = threading.Timer (1, timerFunc) timer.start ()

In the above code, the timer function threading.Timer mainly has two parameters, and the meaning of the parameters is similar to that of solution 1. Next, execute this program:

Hello World~

Process finished with exit code 0

We found that the program was over after only one execution, but it was obviously not the result we wanted. In fact, let's take a look at the Time class. There is an explanatory note like this: Call a function after a specified number of seconds. We find that the above is not executed in a loop, so we need to modify it:

Import threadingdef timerFunc (): print ("Hello World~") global timer timer = threading.Timer (10.5timerFunc) timer.start () timer = threading.Timer (3, timerFunc) timer.start ()

At this point, we can see the output:

Hello World~

Hello World~

Hello World~

...

It should be noted here that the timer must be constructed repeatedly within the timer execution function, because the timer is only executed once and must be called in a loop.

In addition, in the above code, we can actually see that: threading.Timer (5.5, timerFunc), the timer interval unit is seconds, can be floating-point numbers, such as 5.5 timerFunc 0.9, etc., the value given inside and outside the execution function can be different. As in the above example, the first execution of timerFunc is 3 seconds, and the rest is 10.5 seconds later.

Next, let's take a look at how to end the timing function at a certain time. We can use cancel to stop the timer, as in the following example:

Import threadingdef timerFunc (): print ("Hello World~") global timer timer = threading.Timer (10.5, timerFunc) timer.start () timer = threading.Timer (3, timerFunc) timer.start () time.sleep (60) timer.cancel ()

The above code says: after the timer is executed in a certain time, the execution process takes 60 seconds and then stops the timing operation function and exits. The display results are as follows:

Hello World~

Hello World~

Hello World~

Hello World~

Hello World~

...

Process finished with exit code 0

At this point, I believe that everyone on "how to use Python to achieve timing procedures" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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