Python的线程19 拖延症患者Timer类

本文介绍了Python中的Timer类,用于在指定时间后执行任务,类似于时间管理器。示例展示了如何创建和取消Timer,强调了其在多线程中的应用,并提醒读者学习知识要即时行动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正式的Python专栏第56篇,同学站住,别错过这个从0开始的文章!

多线程这一块写了快要20篇了,这篇来一个轻松一点点的。

我们来学习一下Timer类,这个类很简单,但是很特别。

每到新年,大家就会进行总结,跟之前立的flag进行自我复盘。

Timer就是类似一个可以帮助你在立flag之后,到时间告诉你,该进行复盘了。

什么是Timer

准确点说,它是一个延迟执行的事件的单元(threading模块下的一个类),用来在指定时间后执行该事件。

使用上非常简单:

def do_something_after_new_year():
    #新年不🉐️事事顺利,万事如意
    pass
   
#Timer类,传入一个时间(秒数),第二个参数为到点执行的函数。
threading.Timer(60,do_something_after_new_year).start()

这样就设置了一个60秒后执行的程序了。 类似时间管理器,设置多少时间后干嘛

下面就是一个完整可执行的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/1/27 11:16 下午
# @Author : LeiXueWei
# @优快云/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : timer_demo.py
# @Project : hello
import datetime
import threading


def do_after_5_second():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do something now ")


print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
threading.Timer(5, do_after_5_second).start()

运行效果如下:

屏幕快照 2022-01-27 下午11.22.32.png

为了演示方便,这里就设置了等待间隔为5秒,程序很快就结束输出了。

在timer执行的函数中,它是另起一个线程。

Timer 还可以中途取消

我们查看源码可以看到:

屏幕快照 2022-01-27 下午11.33.21.png

这个类也没别的,还有一个cancel的方法。

那么,学委也准备了代码,展示一下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/1/27 11:16 下午
# @Author : LeiXueWei
# @优快云/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : timer_demo2.py
# @Project : hello
import datetime
import threading


def do_after_5_second():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do something now ")


def do_after_1_min():
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("do_after_1_min ")


def stop_another_timer(timer):
    print("%s - thread %s " % (datetime.datetime.now(), threading.current_thread().name))
    print("I will cancel another time")
    print("before cancel - timer :", timer.is_alive())
    timer.cancel()
    print("after cancel - timer :", timer.is_alive())


print("%s - thread %s order a timer" % (datetime.datetime.now(), threading.current_thread().name))
threading.Timer(5, do_after_5_second).start()
print("%s - thread %s order another timer " % (datetime.datetime.now(), threading.current_thread().name))
timer = threading.Timer(60, do_after_1_min)
timer.start()
print("%s - thread %s order another timer " % (datetime.datetime.now(), threading.current_thread().name))
#读者可以注释下方代码,查看前面timer没被中途取消的执行结果。
threading.Timer(10, lambda: stop_another_timer(timer)).start()

下面是运行效果:

屏幕快照 2022-01-27 下午11.36.08.png

我们看到,cancel方法不会把线程的状态(通过is_alive()查看)改变。因为cancel方法改变的是Timer类维护的finished(Event类型,这个学委的另一篇文章也分享过)

最后等候时长的timer被取消了,没有后续执行,程序也就结束了。

总结

很明显,Timer就是一个拖延症患者啊,干啥事都得拖一拖。

希望读者朋友们,别拖延,学习了东西马上学会,马上吸收内化,加油!

喜欢Python的朋友,请关注学委的 Python基础专栏 or Python入门到精通大专栏

持续学习持续开发,我是雷学委!
编程很有趣,关键是把技术搞透彻讲明白。
欢迎关注微信,点赞支持收藏!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雷学委

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值