Python thread 19 procrastination patient Timer class

Posted by jbog91 on Mon, 28 Feb 2022 22:04:54 +0100

Official Python column Article 56, students stop, don't miss this article starting from 0!

Multithreading has been written for nearly 20 articles. This article is a little easier.

Let's learn about the Timer class. This class is very simple, but very special.

Every new year, we will summarize and repeat ourselves with the previous flag.

Timer is a similar one, which can help you set up a flag and tell you that it's time to redo.

What is Timer

To be exact, it is a unit of delayed execution events (a class under the threading module), which is used to execute the event after a specified time.

Very simple to use:

def do_something_after_new_year():
    #New year no 🉐 Everything goes well and everything goes well
    pass
   
#Timer class, pass in a time (seconds), and the second parameter is the function executed to the point.
threading.Timer(60,do_something_after_new_year).start()

In this way, a program to be executed in 60 seconds is set. Similar to the time manager, what happens after setting the time

Here is a complete executable code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 11:16 PM, January 27, 2022
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @XueWeiTag: CodingDemo
# @File : timer_demo.py
# @Project : hello
import datetime
import threading


def do_after_5_second():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do something now ")


print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
threading.Timer(5, do_after_5_second).start()

The operation effect is as follows:

For the convenience of demonstration, the waiting interval is set to 5 seconds, and the program will soon end output.

In the function executed by timer, it is another thread.

Timer can also cancel halfway

We can see from the source code:

There is nothing else about this class. There is also a cancel method.

Then, the school committee also prepared the code to show:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 11:16 PM, January 27, 2022
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @XueWeiTag: CodingDemo
# @File : timer_demo2.py
# @Project : hello
import datetime
import threading


def do_after_5_second():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do something now ")


def do_after_1_min():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do_after_1_min ")


def stop_another_timer(timer):
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("I will cancel another time")
    print("before cancel - timer :", timer.is_alive())
    timer.cancel()
    print("after cancel - timer :", timer.is_alive())


print("%s - thread %s order a timer" % (datetime.datetime.now(), threading.current_thread().name))
threading.Timer(5, do_after_5_second).start()
print("%s - thread %s order another timer " % (datetime.datetime.now(), threading.current_thread().name))
timer = threading.Timer(60, do_after_1_min)
timer.start()
print("%s - thread %s order another timer " % (datetime.datetime.now(), threading.current_thread().name))
#Readers can comment on the code below to see the execution results of the previous timer that has not been cancelled halfway.
threading.Timer(10, lambda: stop_another_timer(timer)).start()

The following is the operation effect:

We can see that the cancel method does not change the state of the thread (viewed through is_live()). Because the cancel method changes the finished type maintained by the Timer class (Event type, which is also shared in another article of the academic committee)

Finally, the timer with a long waiting time is cancelled, and there is no subsequent execution, and the program ends.

summary

Obviously, Timer is a procrastinator. He has to delay everything.

I hope readers, don't delay, learn something immediately, absorb and internalize it immediately, come on!

For those who like Python, please pay attention to the academic committee Python foundation column or Introduction to Python to master the big column

Continuous learning and continuous development, I'm Lei Xuewei!
Programming is very interesting. The key is to understand the technology thoroughly.
Welcome to wechat, like and support collection!

Topics: Python Back-end