python多线程Timer定时器解析,获取调入函数返回值

本文深入解析Python的threading.Timer,包括官方定义和源码分析,并通过示例展示如何在多线程中获取调用函数的返回值。
摘要由CSDN通过智能技术生成

一、解读threading.Timer

1.官方定义

			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

2.源码分析

  class Timer(Thread):  # 继承自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)  # 调用Thread类初始化配置实例
         self.interval = interval  # 在使用Thread类初始化配置实例之后再额外的增加interval属性
         self.function = function  # 同理再额外的增加function属性
         self.args = args if args is not None else [
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值