python — 定时器
一、使用
-
语法:
timer = threading.Timer(interval, function, args timer.start()-
参数说明:
-
interval:定时器时间间隔
-
function:定时器执行的函数,传递函数名
-
args: 传递给定时器函数的参数,必须是个tuple类型。
-
-
示例:
import threading def func(name): print('hello:', name) timer = threading.Timer(interval=1, function=func, args=('cm',)) timer.start()
-
-
定时器说明:
-
python定时器,默认只会执行一次,如果需要一直执行,可以按如下的方式:
import threading def func(name): print('hello:', name) # 3 秒钟定时器 timer = threading.Timer(interval=3, function=func, args=('cm',)) timer.start() # 如果t时候启动的函数是含有参数的,直接在后面传入参数元组 timer = threading.Timer(interval=1, function=func, args=('cm',)) timer.start()
-
本文介绍了Python中使用`threading.Timer`实现定时器的方法。通过设置时间间隔和指定函数,定时器可以在指定时间后执行一次或周期性地执行任务。示例代码展示了如何创建一个延迟1秒和3秒后执行的函数,以及如何传递参数给定时器函数。

7311

被折叠的 条评论
为什么被折叠?



