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单线程实现多个定时器示例
单线程实现多个定时器 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定时器使用示例分享
复制代码 代码如下: 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编写简单的定时器的方法
下面介绍以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 定时器,轮询定时器的实例
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周期性定时器的示例详解
一.用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
-
wxPython定时器wx.Timer简单应用实例
本文实例讲述了wxPython定时器wx.Timer简单应用.分享给大家供大家参考.具体如下: # -*- coding: utf-8 -*- ######################################################## ## 这是wxPython定时器wx.Timer的简单应用 ## testwxTimer1.pyw ######################################################## import wx impo
-
python通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
-
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程序设计有所帮助.
-
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实现定时精度可调节的定时器,供大家参考,具体内容如下 # -* 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使用线程封装的一个简单定时器类实例
本文实例讲述了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.
随机推荐
- DNS服务器管理与配置技巧浅谈
- 详解C++编程中向函数传递引用参数的用法
- jQuery鼠标经过方形图片切换成圆边效果代码分享
- iOS App开发中UIViewController类的使用教程
- js获取当前时间(昨天、今天、明天)
- Repeater控件分别绑定数组和ArrayList实现思路
- asp.net中的cookie使用介绍
- JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)
- asp.net+js实现金额格式化
- PHP解析目录路径的3个函数总结
- 详解Spring Boot 自定义PropertySourceLoader
- JavaWeb实现文件上传与下载实例详解
- Activity/Fragment结束时处理异步回调的解决方案
- C++程序检测内存泄漏的方法分享
- 基于jquery的设置页面文本框 只能输入数字的实现代码
- jQuery插件Tooltipster实现漂亮的工具提示
- javascript获取隐藏dom的宽高 具体实现
- javascript闭包入门示例
- KMP算法的C#实现方法
- php mysql Errcode: 28 终极解决方法
