python编写脚本方法_python 定时器,实现每天凌晨3点执行的方法
如下所示:
'''
Created on 2018-4-20
例子:每天凌晨3點執行func方法
'''
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
# 獲取明天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點執行的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。
總結
以上是生活随笔為你收集整理的python编写脚本方法_python 定时器,实现每天凌晨3点执行的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 办理的保险怎么退保 保险如何退保
- 下一篇: 如何用python绘制灰度直方图_Pyt