zoukankan      html  css  js  c++  java
  • Python threading 单线程 timer重复调用函数

    项目中需要使用定时器,每次都使用构造器函数调用:

    timer = threading.Timer(timerFlag, upload_position)
    timer.start()   
    

    打印线程后发现,每次都会创建一个新的子线程,虽然活跃的线程只有一个,但是也是种资源浪费:

    print("threading active = {} 
       
    ".format(threading.enumerate()))
    
    #打印
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145498161152)>]
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145498161152)>]
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145498161152)>]
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-2, started 123145503416320)>]
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-2, started 123145503416320)>]
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-3, started 123145498161152)>]
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-3, started 123145498161152)>]
    

    阅读源码和文档

    class Timer(Thread):
        """Call a function after a specified number of seconds:
    
                t = Timer(30.0, f, args=None, kwargs=None)
                t.start()
                t.cancel()     # stop the timer's action if it's still waiting
    
        """
    
        def __init__(self, interval, function, args=None, kwargs=None):
            Thread.__init__(self)
            self.interval = interval
            self.function = function
            self.args = args if args is not None else []
            self.kwargs = kwargs if kwargs is not None else {}
            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()
    
    # Special thread class to represent the main thread
    # This is garbage collected through an exit handler
    

    发现,其实Timer是threading的子类,用wait实现了定时效果,绑定了入参function,于是修改代码如下

    
    def startTimer():
        global timer
        if timer != None:
            timer.finished.wait(timerFlag)
            timer.function()   
        else:
            timer = threading.Timer(timerFlag, upload_position)
            timer.start()
    

    打印结果:

    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    
    
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    
    threading active = [<_MainThread(MainThread, stopped 140735596835712)>, <Timer(Thread-1, started 123145516048384)>]
    

    始终只有一个线程且重复调用函数方法~End~

       
       
       

    友情链接:

    个人网站       技术博客        简书主页

  • 相关阅读:
    源码安装mysql-5.7.13一周的冤枉路总结。满满的都是泪啊
    一键安装Apache服务脚本
    源码编译安装LAMP
    Vue侦听器 watch
    Vue计算属性 computed
    Vue表单的值绑定和修饰符
    js编码解码decodeURI(URIstring)与decodeURIComponent(URIstring)的区别
    常用的JS表单验证
    js正则表达式匹配手机号中间四位以及匹配姓名第一个字符,将其替换为*
    Vue按键修饰符,鼠标按钮修饰符
  • 原文地址:https://www.cnblogs.com/tig666666/p/9300973.html
Copyright © 2011-2022 走看看