python定时执行程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import datetime import threading
def func(): print("haha") timer = threading.Timer(86400, func) timer.start()
now_time = datetime.datetime.now()
next_time = now_time + datetime.timedelta(days=+1) next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day
next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S")
timer_start_time = (next_time - now_time).total_seconds() print(timer_start_time)
timer = threading.Timer(timer_start_time, func) timer.start()
|