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

yipeiwu_com6年前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破解zip加密文件的方法

python破解zip加密文件的方法

首先我们先来桌面创建一个文件 我们创建了一个名为q的txt文件然后我们将它压缩,压缩的时候记得设置上密码 我这边将密码设置为123456, 接下来我们打开我们的编写工具,开始...

python批量修改图片尺寸,并保存指定路径的实现方法

如下所示: import os from PIL import Image filename = os.listdir("D:\\Work\\process\\样本处理\\pol...

python使用pymysql实现操作mysql

pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。 适用环境 python版本 &...

Python单元测试简单示例

本文实例讲述了Python单元测试方法。分享给大家供大家参考,具体如下: Eric书中《Python编程从入门到实践》中的一个例子。 《Python编程从入门到实践》随书源码可至此下载:...

Django 表单模型选择框如何使用分组

Django 表单模型选择框如何使用分组

起步 Django 表单中有两种字段类型可以使用选择框: ChoiceField 和 ModelChoiceField 。 对于 ChoiceField 的基本使用是: class...