Introduction to the method of implementing loop timer in Python (with code)

不言
Release: 2019-03-14 11:13:28
forward
16978 people have browsed it

This article brings you an introduction to the method of implementing a loop timer in python (with code). It has a certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

Python How to write a timer to perform a certain operation in a loop?

Timer object

from threading import Timer
def hello(): 
    print "hello, world" 
   
t = Timer(10.0, hello) 
t.start()
Copy after login

Output after 10 seconds:

hello, world
Copy after login

Focus on the code t = Timer(10.0, hello), python provides a Timer object, it will Perform an operation after the specified time; its full form:

class threading.Timer(interval, function, args=[], kwargs={})
Copy after login

interval is the time interval, function is a callable object, and args and kwargs will be used as parameters of function.

Note: The function will only be executed once and will not be executed regularly, and Timer will create a new thread when executing the operation.

There is a slight difference between Timer in python2 and python3:

# python2.7
def Timer(*args, **kwargs):
    return _Timer(*args, **kwargs)
# python3.7
class Timer(Thread):
    pass
Copy after login

In python3, Timer is a subclass of Thread; in python2, _Timer is a subclass of Thread, and Timer is just a factory method of the _Timer class.

The above code will only print hello, world once and then exit. So how to print at intervals in a loop?

Rough loop timer

One way is to continue to register a Timer in the function, so that the function can continue to be executed at the next interval;

from threading import Timer
def hello(): 
    print "hello, world" 
    Timer(10.0, hello) .start()

t = Timer(10.0, hello) 
t.start()
Copy after login

Every 10 seconds Output a hello, world.

The effect is achieved, but there seems to be some problem here. Back to the Timer itself, it is a thread. Every time the cycle interval is operated, the system has to create a thread and then recycle it, which is very expensive for the system. If the time interval interval is very short, the system will create many threads at once, and these threads are difficult to recycle quickly, causing system memory and CPU resources to be consumed.
So it is not recommended to continue to register a Timer in the function.

More pythonic loop timer

Here is a more pythonic method:

from threading import _Timer
def hello():
     print "hello, world"
class RepeatingTimer(_Timer): 
    def run(self):
        while not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
            self.finished.wait(self.interval)
t = RepeatingTimer(10.0, hello)
t.start()
Copy after login

Focus on the RepeatingTimer class, which inherits threading._Timer, but rewrites the parent class run method. This is how Python2 is written. RepeatingTimer in python3 should inherit threading.Timer.

Why should we override the run method of Thread?

_Timer is a Thread subclass. Let’s first look at the run usage of the Thread class.

from threading import Thread
def hello():
     print "hello, world"
# 继承 Thread
class MyThread(Thread):
    # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
    def run(self):
        hello()
t = MyThread()
t.start()
Copy after login

Complete definition of Thread object:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
Copy after login

The run method code:

class Thread(_Verbose):
    def run(self):
        try:
            if self.__target:
                self.__target(*self.__args, **self.__kwargs)
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self.__target, self.__args, self.__kwargs
Copy after login

The standard run method is used to execute the target method passed in by the user to the constructor. Subclasses can override the run method and write the code to be executed into run. After the thread is created, the run() method will be run when the user calls the start() method.

So RepeatingTimer overrides the run() method of _Timer, which can change the execution body of the thread. When we call the start() method of RepeatingTimer, the run() method we rewrite will be executed.

Look at the while not self.finished.is_set() statement in the RepeatingTimer class. self.finished.is_set() will not exit the loop until True and the timer will end. finished is a threading.Event object. An Event object manages a flag, which can be set to True by the set() method or set to False by the clear() method. When calling wait([timeout]), the thread will sleep until the flag is True or the timeout expires. .

We know that the timer has a cancel() method that can cancel the operation in advance. It actually calls the Event.clear() method to let the wait method end waiting in advance, and determines that the timer operation will not be performed when the flag is true. Specific code:

class _Timer(Thread):
    """Call a function after a specified number of seconds:
            t = Timer(30.0, f, args=[], kwargs={})
            t.start()
            t.cancel() # stop the timer's action if it's still waiting
    """

    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()

    def cancel(self):
        """Stop the timer if it hasn't finished yet"""
        self.finished.set()

    def run(self):
        self.finished.wait(self.interval)
        if not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
        self.finished.set()
Copy after login

So the run method of RepeatingTimer will always execute the while loop body. In the loop body, the function object passed in by the user will be executed and wait for the specified time. When the user wants to exit the timer, he only needs to call the cancel method and set the flag to True so that the loop body will not continue to execute. This completes a pretty good loop timer.

The above is the detailed content of Introduction to the method of implementing loop timer in Python (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template