search
HomeBackend DevelopmentPython TutorialIntroduction to the method of implementing loop timer in Python (with code)

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()

Output after 10 seconds:

hello, world

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={})

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

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()

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()

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()

Complete definition of Thread object:

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

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

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()

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
The Main Purpose of Python: Flexibility and Ease of UseThe Main Purpose of Python: Flexibility and Ease of UseApr 17, 2025 am 12:14 AM

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python: The Power of Versatile ProgrammingPython: The Power of Versatile ProgrammingApr 17, 2025 am 12:09 AM

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Learning Python in 2 Hours a Day: A Practical GuideLearning Python in 2 Hours a Day: A Practical GuideApr 17, 2025 am 12:05 AM

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

Python vs. C  : Pros and Cons for DevelopersPython vs. C : Pros and Cons for DevelopersApr 17, 2025 am 12:04 AM

Python is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.

Python: Time Commitment and Learning PacePython: Time Commitment and Learning PaceApr 17, 2025 am 12:03 AM

The time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.

Python: Automation, Scripting, and Task ManagementPython: Automation, Scripting, and Task ManagementApr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version