定时器 就是隔多长时间去触发任务执行

指定n秒后执行某操作

 

Timer如何使用,看Timer源码

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

 

Timer() 

interval 第一个参数传 间隔时间

function  传执行任务的函数  隔了多少秒后执行这个函数

给函数传参方式 args   kwargs 

 

Timer用的是Thread模块,每启动一个定时器,启动一个线程

Thread.__init__(self)

 

 

5秒后启动线程

from threading import Timer


def task(name):
    print("helo %s" %name)

t = Timer(5, task, args=("mike",))

# 5秒后启动线程
t.start()

 

相关文章:

  • 2022-01-03
  • 2021-12-08
  • 2022-02-23
  • 2021-06-03
猜你喜欢
  • 2022-01-13
  • 2021-06-29
  • 2021-06-09
  • 2021-12-04
  • 2021-12-04
  • 2021-12-04
  • 2021-05-03
相关资源
相似解决方案