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设计】。

相关文章

用vue.js组件模拟v-model指令实例方法

1、问题描述 在使用v-model指令实现输入框数据双向绑定,输入值时对应的这个变量的值也随着变化;但是这里不允许使用v-model,需要写一个组件实现v-model指令效果 <...

Python实现模拟分割大文件及多线程处理的方法

本文实例讲述了Python实现模拟分割大文件及多线程处理的方法。分享给大家供大家参考,具体如下: #!/usr/bin/env python #--*-- coding:utf-8...

Python全排列操作实例分析

本文实例讲述了Python全排列操作。分享给大家供大家参考,具体如下: step 1: 列表的全排列: 这个版本比较low # -*-coding:utf-8 -*- #!pytho...

利用python实现简易版的贪吃蛇游戏(面向python小白)

利用python实现简易版的贪吃蛇游戏(面向python小白)

引言 作为python 小白,总是觉得自己要做好百分之二百的准备,才能开始写程序。以至于常常整天在那看各种语法教程,学了几个月还是只会print('hello world')。 这样...

Python 提取dict转换为xml/json/table并输出的实现代码

核心代码: #!/usr/bin/python #-*- coding:gbk -*- #设置源文件输出格式 import sys import getopt import json...