zoukankan      html  css  js  c++  java
  • python 并发编程 多线程 定时器

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

    指定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()
  • 相关阅读:
    leetcode643.滑动窗口例题
    BZOJ4195 离散化+并查集
    luogu线性表刷题
    2021-5-29 周报博客
    2021-5-28 日报博客
    2021-5-27 日报博客
    2021-5-26 日报博客
    2021-5-25 日报博客
    2021-5-24 日报博客
    梦断代码阅读笔记之二
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/11074039.html
Copyright © 2011-2022 走看看