Python 多线程定时器

一次 timer 只生效一次,不会反复循环,如果实现循环触发,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
import time
import threading

def createTimer():
t = threading.Timer(2, repeat)
t.start()

def repeat():
print('Now:', time.strftime('%H:%M:%S',time.localtime()))
createTimer()

createTimer()

这段代码的功能就是每 2 秒打印出当前的时间,即一个 2 秒的定时器。


Python 多线程定时器
https://wonderhoi.com/2025/03/25/Python-多线程定时器/
作者
wonderhoi
发布于
2025年3月25日
许可协议