“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
Timer concept
What is a timer? It triggers an event after a specified time, starting from a specified time. Users can customize the period and frequency of the timer.
Implement a simple timing program
Plan a
How do you define a timer function in Python? Let’s look at the first method first. Suppose we need to execute a function, userCountFunc, that needs to be executed every hour. So, we can write it like this:
def main() :
startCronTask(userCountFunc, minutes=60)
if __name__ == '__main__':
main()
Copy the code
In the code above, we define a timing function startCronTask after defining a main function. The first argument is the name of the function, and the second argument is the time. The second argument indicates how long it takes to call the function with the first argument. Note that the first argument is a function object, pass the argument, use the function name (such as userCountFunc) to represent the object, not the function execution statement userCountFunc(), otherwise an error will be reported. So, to implement this function, we need to introduce timing. Python has a timed task module BlockingScheduler:
from apscheduler.schedulers.blocking import BlockingScheduler
def startCronTask(task, **config) :
# BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(task, 'interval', **config)
scheduler.start()
Copy the code
Once a scheduling module is defined, the actual scheduling function is complete. Next, we need to implement the scheduled logical function userCountFunc:
def userCountFunc() :
logger.info('count user')...Copy the code
Thus, for scenario 1, the simple timing function implemented is complete.
Scheme 2
In Python, you can use BlockingScheduler to implement timer timer. In Python, you can also use BlockingScheduler to implement timer.
import threading
def timerFunc() :
print('Hello World~')
timer = threading.Timer(1, timerFunc)
timer.start()
Copy the code
In the code above, the threading.timer function takes two main arguments, which have similar meanings to solution 1.
Hello World~
Process finished with exit code 0
Copy the code
We found that after just one execution, the program was over, but obviously not what we wanted. Call a function after a specified number of seconds Call a function after a specified number of seconds
import threading
def timerFunc() :
print('Hello World~')
global timer
timer = threading.Timer(10.5, timerFunc)
timer.start()
timer = threading.Timer(3, timerFunc)
timer.start()
Copy the code
At this point, we can see the output:
Hello World~
Hello World~
Hello World~
...
Copy the code
The important thing to note here is that the construction of the timer must be repeated inside the timer execution function, because the timer is only executed once after construction and must be called in a loop.
Timer(5.5, timerFunc). The Timer interval is in seconds. It can be a floating-point number such as 5.5, 0.9, etc. The value given can be different inside and outside the execution function timerFunc. In the example above, the first timerFunc is executed after 3 seconds, and the subsequent timerFunc is executed after 10.5 seconds.
Next, let’s look at how to end the timing function at a certain time. We can use cancel to stop the timer from working, as in the following example:
import threading
def 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()
Copy the code
The preceding code indicates that after the timer is executed at a certain time, the scheduled operation is stopped after 60 seconds. The displayed result is:
Hello World~
Hello World~
Hello World~
Hello World~
Hello World~
...
Process finished with exit code 0
Copy the code