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

相关文章

使用Python3+PyQT5+Pyserial 实现简单的串口工具方法

使用Python3+PyQT5+Pyserial 实现简单的串口工具方法

练手项目,先上图 先实现一个简单的串口工具,为之后的上位机做准备 代码如下: github 下载地址 pyserial_demo.py import sys import seri...

python安装numpy和pandas的方法步骤

最近要对一系列数据做同比比较,需要用到numpy和pandas来计算,不过使用python安装numpy和pandas因为linux环境没有外网遇到了很多问题就记下来了。首要条件,pyt...

python 利用文件锁单例执行脚本的方法

你可能会遇到这样的要求,一个脚本,只允许有一个实例。 在python中,为了实现这个需求,可以引入fcntl模块对文件加一个排他锁,这样一来,先启动的实例拥有了文件锁,而后启动的实例则因...

Odoo中如何生成唯一不重复的序列号详解

前言 最近在做的项目中有一个需求是要让某个字段值根据记录产生的日期和一定的组合规则按顺序生成一个序列号,这个序列号不可重复,这原本是一个很常见的需求,没有多想就写好了。由于没有考虑到并发...

Anaconda之conda常用命令介绍(安装、更新、删除)

Anaconda之conda常用命令介绍(安装、更新、删除)

anaconda用法: 查看已经安装的包: pip list 或者 conda list 安装和更新: pip install requests pip install request...