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使用线程封装的一个简单定时器类实例
本文实例讲述了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.
-
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周期性定时器的示例详解
一.用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)用法.分享给大家供大家参考.具体如下: # encoding: UTF-8 import threading #Timer(定时器)是Thread的派生类, #用于在指定时间后调用一个方法. def func(): print 'hello timer!' timer = threading.Timer(5, func) timer.start() 该程序可实现延迟5秒后调用func方法的功能. 希望本文所述对大家的Python程序设计有所帮助.
-
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实现定时精度可调节的定时器,供大家参考,具体内容如下 # -* 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编写简单的定时器的方法
下面介绍以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通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
-
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
随机推荐
- java MyBatis拦截器Inteceptor详细介绍
- Windows下ORACLE 10g完全卸载的方法分析
- javascript实现修改微信分享的标题内容等
- Python基础知识_浅谈用户交互
- 深入分析golang多值返回以及闭包的实现
- Android UI系列-----Dialog对话框示例
- mysql5.7.19 zip 详细安装过程和配置
- 完美解决android M上锁屏情况下,禁止pc通过MTP访问手机存储单元
- xampp修改mysql默认密码的方法
- php实现修改新闻时删除图片的方法
- 批处理的图形界面实现方法小结
- php array_reverse 以相反的顺序返回数组实例代码
- Android 仿京东、拼多多商品分类页的示例代码
- android开发 eclipse alt+”/”自动提示失效的解决方法
- 使用remalloc的注意事项说明(必看篇)
- 记录Android微信分享功能的吐槽与思考
- PHP+mysql防止SQL注入的方法小结
- 使用Python监控文件内容变化代码实例
- java解析Excel的方法(xls、xlsx两种格式)
- 从零搭一个自用的前端脚手架的方法步骤