首页 > 编程语言 > 详细

python 并发编程 多线程 定时器

时间:2019-07-06 16:57:36      阅读:94      评论:0      收藏:0      [点我收藏+]

 

 

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

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

 

python 并发编程 多线程 定时器

原文:https://www.cnblogs.com/mingerlcm/p/11074039.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!