如下所示:
'''Created on 2018-4-20例子:每天凌晨3點執行func方法'''import datetimeimport threadingdef 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().yearnext_month = next_time.date().monthnext_day = next_time.date().day# 獲取明天3點時間next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S")# # 獲取昨天時間# last_time = now_time + datetime.timedelta(days=-1)# 獲取距離明天3點時間,單位為秒timer_start_time = (next_time - now_time).total_seconds()print(timer_start_time)# 54186.75975#定時器,參數為(多少時間后執行,單位為秒,執行的方法)timer = threading.Timer(timer_start_time, func)timer.start()以上這篇python 定時器,實現每天凌晨3點執行的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答