Timer Concept
What is a timer? It refers to starting from a specified moment, elapsing a specified time, and then triggering an event. Users can customize the period and frequency of the timer.
Implement a simple timer program
Option 1
How do you define a timer function in Python? Let's look at the first method. Suppose we need to execute a function, userCountFunc, which needs to be executed every hour. Then, we can write it like this.
def main(): startCronTask(userCountFunc, minutes=60) if __name__ == '__main__': main()
Such as the above code, we defined a main function, then defined a timed function startCronTask. the first parameter for the function name, the second parameter for the time, the second parameter indicates how long after the first parameter to call the function after the first parameter. The first parameter is a function object, for parameter passing, with the function name (such as userCountFunc) that the object, not the function executes the statement userCountFunc (), otherwise it will report an error. Then, in the implementation of this function, you need to introduce the timing function, Python has a timed task module BlockingScheduler.
from import BlockingScheduler def startCronTask(task, **config): # BlockingScheduler scheduler = BlockingScheduler() scheduler.add_job(task, 'interval', **config) ()
After defining a scheduling module, the actual timing scheduling function is complete. Next, you need to implement the logic function userCountFunc.
def userCountFunc(): ('count user') ...
In this way, for option 1, the simple timing function implemented is complete.
Option 2
Option 1 is introduced in Python comes with the BlockingScheduler module, Python in addition to BlockingScheduler, but also through the thread to achieve the timer timer, to simply look at the following code.
import threading def timerFunc(): print('Hello World~') timer = (1, timerFunc) ()
In the above code, the timer function has two main parameters, the significance of the parameters is similar to the first program, the next execution of this program:
Hello World~
Process finished with exit code 0
We find that the program ends after only one execution, but obviously not the result we want. In fact, we look at the Time class, there is such an explanatory note: Call a function after a specified number of seconds, we found that the above after the execution did not loop, so we need to modify the following.
import threading def timerFunc(): print('Hello World~') global timer timer = (10.5, timerFunc) () timer = (3, timerFunc) ()
At this point, we can see the output:
Hello World~
Hello World~
Hello World~
...
It is important to note here that the timer must be constructed repeatedly inside the timer execution function, because the timer is only executed 1 time after construction and must be called cyclically.
In addition, in the above code, we can actually see: (5.5, timerFunc), the timer interval unit is seconds, can be a floating-point number, such as 5.5, 0.9, etc., in the implementation of the function timerFunc internal and external to the value can be different. For example, in the above example, the first execution of timerFunc is after 3 seconds, and the following ones are after 10.5 seconds.
Next, let's see how to end the timer function at a certain time again. We can use cancel to stop the timer as in the following example:
import threading def timerFunc(): print('Hello World~') global timer timer = (10.5, timerFunc) () timer = (3, timerFunc) () (60) ()
The above code indicates that after the timer is executed according to a certain time, the execution process takes 60 seconds to stop the timed operation function and exit. The display result is:
Hello World~
Hello World~
Hello World~
Hello World~
Hello World~
...Process finished with exit code 0
To this point, this article on the use of Python to achieve the method of timing program is introduced to this article, more related Python timer program content please search my previous articles or continue to browse the following related articles I hope that you will support me in the future more!