python 定时器,实现每天凌晨3点执行的方法

yipeiwu_com5年前Python基础

如下所示:

'''
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中import reload __import__的区别详解

import 作用:导入/引入一个python标准模块,其中包括.py文件、带有__init__.py文件的目录(自定义模块)。 import module_name[,module...

基于Python中求和函数sum的用法详解

基于Python中求和函数sum的用法详解 今天在看《集体编程智慧》这本书的时候,看到一段Python代码,当时是百思不得其解,总觉得是书中排版出错了,后来去了解了一下sum的用法,看了...

python实现网站的模拟登录

本文主要用python实现了对网站的模拟登录。通过自己构造post数据来用Python实现登录过程。 当你要模拟登录一个网站时,首先要搞清楚网站的登录处理细节(发了什么样的数据,给谁发等...

python批量识别图片指定区域文字内容

Python批量识别图片指定区域文字内容,供大家参考,具体内容如下 简介 对于一张图片,需求识别指定区域的内容 1.截取原始图上的指定图片当做模板 2.根据模板相似度去再原始图片上识别准...

Python XlsxWriter模块Chart类用法实例分析

Python XlsxWriter模块Chart类用法实例分析

本文实例讲述了Python XlsxWriter模块Chart类用法。分享给大家供大家参考,具体如下: 一 点睛 Chart类是XlsxWriter模块中图表组件的基类,支持的图表类型包...