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
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools