import threading
from datetime import datetime
def do_task():
print "%s: 任务被触发" % datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
def start_timer(interval):
do_task()
TIMER = threading.Timer(interval, start_timer, [interval])
TIMER.start()
if __name__ == "__main__":
start_timer(5)
注意看任务执行时间 每隔五秒运行一次:
2022-02-25T18:19:25: 任务被触发 2022-02-25T18:19:30: 任务被触发 2022-02-25T18:19:35: 任务被触发 2022-02-25T18:19:40: 任务被触发 2022-02-25T18:19:45: 任务被触发 2022-02-25T18:19:50: 任务被触发 2022-02-25T18:19:55: 任务被触发 2022-02-25T18:20:00: 任务被触发 2022-02-25T18:20:05: 任务被触发 2022-02-25T18:20:10: 任务被触发 2022-02-25T18:20:15: 任务被触发 2022-02-25T18:20:20: 任务被触发 2022-02-25T18:20:25: 任务被触发 2022-02-25T18:20:30: 任务被触发 2022-02-25T18:20:35: 任务被触发 2022-02-25T18:20:40: 任务被触发
|