摘要:
下文讲述Python中设置定时任务的方法分享,如下所示;
下文将一一道来,如下所示:
实现思路:
使用threading.Timer函数间隔指定时间运行函数
或使用while 无限循环 当时间达到指定位置时,
运行相应函数
例:
Python 定时任务的示例分享
#maomao365.com
#Python 间隔8秒运行程序的示例分享
import threading
def funTest():
print("开始执行...")
timer = threading.Timer(5, funTest)
timer.start()
当时间达到指定时间运行程序
#maomao365.com
#Python 指定时间运行的示例分享
import time
while True:
c = time.strftime("%H:%M:%S", time.localtime()) # 刷新服务器时间
if c == "0:00:01": #设置每天定时的时间
print('定时任务开始执行....')
s = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "运行指定任务"
print(s)
#暂停10秒
time.sleep(10)