定时器 sched模块

import sched
import time
from datetime import datetime
# 初始化sched模块的scheduler类
# 第一个参数是一个可以返回时间戳的函数,第二参数可以在定时未到达之前阻塞
schdule = sched.scheduler(time.time, time.sleep)
# 被周期性调度触发函数
def printTime(inc):
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    schedule.enter(inc, 0, printTime, (inc,))
# 默认参数60s
def main(inc=60):
    # enter四个参数分别为:间隔事件,优先级(用于同时到达两个事件同时执行的顺序),被调度触发的函数,给该触发器函数的参数(tuple形式)
    schedule.enter(0, 0, pirntTime, (inc,))
    schedule.run()
while 1:
    main(5)

参考:

https://www.jianshu.com/p/403bcb57e5c2

原文地址:https://www.cnblogs.com/gexbooks/p/12932394.html