python多线程中的定时器你了解吗
作者:fnwfh 时间:2021-09-01 23:39:21
定时器
定时器:每隔一段时间启动一次线程
threading.Timer
创建的是一个线程!定时器基本上都是在线程中执行
创建定时器:
threading.Timer(interval, function, args=None, kwargs=None)
interval
— 定时器间隔,间隔多少秒之后启动定时器任务(单位:秒);function — 线程函数;args — 线程参数,可以传递元组类型数据,默认为空(缺省参数);
kwargs
— 线程参数,可以传递字典类型数据,默认为空(缺省参数);
使用threading的 Timer 类
start() 为通用的开始执行方法cancel ()为取消执行的方法
使用定时器实现当前时间每秒循环输出一次
# 使用定时器实现当前时间每秒循环输出一次
import threading
import time
def run():
strtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) #当前时间
print(strtime)
t1 = threading.Timer(1, function=run) #每1秒钟启动一次run函数.不是下面的t启动的
t1.start()
if __name__ == '__main__':
t=threading.Timer(1,function=run) #1秒,target换成了function
t.start()
注:t启动一次后,是由t1再次启动并循环(每1秒钟启动一次)
python多线程定时器
来源:https://blog.csdn.net/qq_54640399/article/details/122421799
标签:python,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python math模块使用方法介绍
2021-06-20 23:44:01
简单了解python中的与或非运算
2021-03-20 01:45:02
![](https://img.aspxhome.com/file/2023/5/67915_0s.png)
MySQL百万级高并发网站实战攻略
2009-03-25 15:49:00
Insert into与AddNew哪一个更好?
2009-10-28 18:30:00
pytorch中使用LSTM详解
2021-01-08 04:27:10
![](https://img.aspxhome.com/file/2023/4/89394_0s.jpg)
Python Queue模块详细介绍及实例
2022-03-08 11:03:58
使用ACCESS做网络版程序的四种解决方案
2009-01-14 16:22:00
怎么用FSO生成一个UNICODE格式的文本文件
2009-05-26 15:40:00
Python网络编程之ftplib模块
2021-12-11 01:49:37
PyQt5实现进度条与定时器及子线程同步关联
2023-03-20 14:48:35
![](https://img.aspxhome.com/file/2023/5/88875_0s.gif)
Dreamweaver制作技巧四则
2008-01-04 09:42:00
Django 通过JS实现ajax过程详解
2023-08-17 08:00:46
JSP安全开发之XSS漏洞详解
2023-06-13 13:07:24
![](https://img.aspxhome.com/file/2023/1/62781_0s.png)
用Python复现二战德军enigma密码机
2022-04-12 23:22:59
![](https://img.aspxhome.com/file/2023/1/76191_0s.png)
利用python Selenium实现自动登陆京东签到领金币功能
2021-11-09 12:00:33
![](https://img.aspxhome.com/file/2023/8/72258_0s.jpg)
用来武装Firefox的24款Web开发插件
2010-02-28 12:40:00
Asp+Sql 对数据库的各种操作
2007-09-22 10:38:00
python解释模型库Shap实现机器学习模型输出可视化
2022-04-27 08:00:10
![](https://img.aspxhome.com/file/2023/8/78518_0s.png)
最全的MYSQL备份方法
2009-12-29 10:19:00
微信小程序wxml列表渲染原理解析
2023-07-17 14:28:08