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定时器使用示例分享
复制代码 代码如下: class SLTimer(multiprocessing.Process): #from datetime import datetime #import time def __init__(self, target=None, args=(), kwargs={},date=None,time=None): '''\ @param date 1900-01-01 @param time 00:00:00
-
python通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
-
用Python编写简单的定时器的方法
下面介绍以threading模块来实现定时器的方法. 首先介绍一个最简单实现: import threading def say_sth(str): print str t = threading.Timer(2.0, say_sth,[str]) t.start() if __name__ == '__main__': timer = threading.Timer(2.0,say_sth,['i am here too.']) timer.start() 不清楚在某些特殊应用场景下有什么缺陷
-
python单线程实现多个定时器示例
单线程实现多个定时器 NewTimer.py 复制代码 代码如下: #!/usr/bin/env python from heapq import *from threading import Timerimport threadingimport uuidimport timeimport datetimeimport sysimport math global TimerStampglobal TimerTimes class CancelFail(Exception): pass c
-
Python实现定时精度可调节的定时器
本文实例为大家分享了Python实现定时精度可调节的定时器,供大家参考,具体内容如下 # -* coding: utf-8 -*- import sys import os import getopt import threading import time def Usage(): usage_str = '''''说明: \t定时器 \timer.py -h 显示本帮助信息,也可以使用--help选项 \timer.py -d num 指定一个延时时间(以毫秒为单位) \t 也可以使用--d
-
对python周期性定时器的示例详解
一.用thread实现定时器 py_timer.py文件 #!/usr/bin/python #coding:utf-8 import threading import os import sys class _Timer(threading.Thread): def __init__(self, interval, function, args=[], kwargs={}): threading.Thread.__init__(self) self.interval = interval se
-
python定时器(Timer)用法简单实例
本文实例讲述了python定时器(Timer)用法.分享给大家供大家参考.具体如下: # encoding: UTF-8 import threading #Timer(定时器)是Thread的派生类, #用于在指定时间后调用一个方法. def func(): print 'hello timer!' timer = threading.Timer(5, func) timer.start() 该程序可实现延迟5秒后调用func方法的功能. 希望本文所述对大家的Python程序设计有所帮助.
-
wxPython定时器wx.Timer简单应用实例
本文实例讲述了wxPython定时器wx.Timer简单应用.分享给大家供大家参考.具体如下: # -*- coding: utf-8 -*- ######################################################## ## 这是wxPython定时器wx.Timer的简单应用 ## testwxTimer1.pyw ######################################################## import wx impo
-
python 定时器,轮询定时器的实例
python 定时器默认定时器只执行一次,第一个参数单位S,几秒后执行 import threading def fun_timer(): print('Hello Timer!') timer = threading.Timer(1, fun_timer) timer.start() 改成以下可以执行多次 建立loop_timer.py from threading import _Timer class LoopTimer(_Timer): """Call a funct
-
Python定时器实例代码
在实际应用中,我们经常需要使用定时器去触发一些事件.Python中通过线程实现定时器timer,其使用非常简单.看示例: import threading def fun_timer(): print('Hello Timer!') timer = threading.Timer(1, fun_timer) timer.start() 输出结果: Hello Timer! Process finished with exit code 0 注意,只输出了一次,程序就结束了,显然不是我们想要的结果
-
python使用线程封装的一个简单定时器类实例
本文实例讲述了python使用线程封装的一个简单定时器类.分享给大家供大家参考.具体实现方法如下: from threading import Timer class MyTimer: def __init__(self): self._timer= None self._tm = None self._fn = None def _do_func(self): if self._fn: self._fn() self._do_start() def _do_start(self): self.
随机推荐
- jQuery实现简单漂亮的Nav导航菜单效果
- AngularJS 实现点击按钮获取验证码功能实例代码
- Linux下用SSH退出符切换SSH会话的实现方法
- java读取resource目录下文件的方法示例
- 利用Python爬取微博数据生成词云图片实例代码
- python实现微信接口(itchat)详细介绍
- ASP.NET简单获取服务端和客户端计算机名称的方法
- 基于php伪静态的实现详细介绍
- PHP闭包函数传参及使用外部变量的方法
- JSP由浅入深(3)—— 通过表达式增加动态内容
- C++堆排序算法实例详解
- Vue.js教程之计算属性
- 第一次接触Bootstrap框架
- javascript刷新父页面的各种方法汇总
- 支持IE,Firefox的javascript 日历控件
- Rcmd.vbs [Remote Cmd with wmi]远程脚本
- Linux中logrotate日志轮询操作总结
- sql rollup用法 小计汇总
- Java异常详解_动力节点Java学院整理
- 浅谈Android App开发中Fragment的创建与生命周期
