from threading import Timer
def hello():
print("hello, world")
# 指定10秒后执行hello函数
t = Timer(10.0, hello)
t.start()
上面程序使用 Timer 控制 10s 后执行 hello 函数。
from threading import Timer
import time
# 定义总共输出几次的计数器
count = 0
def print_time():
print("当前时间:%s" % time.ctime())
global t, count
count += 1
# 如果count小于10,开始下一次调度
if count < 10:
t = Timer(1, print_time)
t.start()
# 指定1秒后执行print_time函数
t = Timer(1, print_time)
t.start()
上面程序开始运行后,程序控制 1s 后执行 print_time() 函数。print_time() 函数中的代码会进行判断,如果 count 小于 10,程序再次使用 Timer 调度 1s 后执行 print_time() 函数,这样就可以控制 print_time() 函数多次重复执行。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有