Python 实现某个功能每隔一段时间被执行一次的功能方法


Posted in Python onOctober 14, 2018

本人在做项目的时候遇到一个问题:

某个函数需要在每个小时的 3 分钟时候被执行一次,我希望我 15:45 启动程序,过了18 分钟在 16:03 这个函数被执行一次,下一次过 60 分钟在 17:03 再次被执行,下一次 18:03,以此类推。

以下是我基于 Timer 做的再封装实现了此功能。

# -*- coding: utf-8 -*-
# ==================================================
# 对 Timer 做以下再封装的目的是:当某个功能需要每隔一段时间被
# 执行一次的时候,不需要在回调函数里对 Timer 做重新安装启动
# ==================================================
__author__ = 'liujiaxing'

from threading import Timer
from datetime import datetime

class MyTimer( object ):

 def __init__( self, start_time, interval, callback_proc, args=None, kwargs=None ):

  self.__timer = None
  self.__start_time = start_time
  self.__interval = interval
  self.__callback_pro = callback_proc
  self.__args = args if args is not None else []
  self.__kwargs = kwargs if kwargs is not None else {}

 def exec_callback( self, args=None, kwargs=None ):
  self.__callback_pro( *self.__args, **self.__kwargs )
  self.__timer = Timer( self.__interval, self.exec_callback )
  self.__timer.start()

 def start( self ):
  interval = self.__interval - ( datetime.now().timestamp() - self.__start_time.timestamp() )
  print( interval )
  self.__timer = Timer( interval, self.exec_callback )
  self.__timer.start()

 def cancel( self ):
  self.__timer.cancel() 
  self.__timer = None

class AA:
 def hello( self, name, age ):
  print( "[%s]\thello %s: %d\n" % ( datetime.now().strftime("%Y%m%d %H:%M:%S"), name, age ) )

if __name__ == "__main__":

 aa = AA()
 start = datetime.now().replace( minute=3, second=0, microsecond=0 )
 tmr = MyTimer( start, 60*60, aa.hello, [ "owenliu", 18 ] )
 tmr.start()
 tmr.cancel()

以上这篇Python 实现某个功能每隔一段时间被执行一次的功能方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用paramiko实现远程拷贝文件的方法
Apr 18 Python
python实现可以断点续传和并发的ftp程序
Sep 13 Python
python的中异常处理机制
Aug 30 Python
python钉钉机器人运维脚本监控实例
Feb 20 Python
Python API 自动化实战详解(纯代码)
Jun 11 Python
Django urls.py重构及参数传递详解
Jul 23 Python
django中使用POST方法获取POST数据
Aug 20 Python
Python表达式的优先级详解
Feb 18 Python
python实现银行实战系统
Feb 26 Python
使用python实现名片管理系统
Jun 18 Python
Python绘制组合图的示例
Sep 18 Python
详解Python牛顿插值法
May 11 Python
Python 实现异步调用函数的示例讲解
Oct 14 #Python
python 异或加密字符串的实例
Oct 14 #Python
python实现换位加密算法的示例
Oct 14 #Python
浅析Python 3 字符串中的 STR 和 Bytes 有什么区别
Oct 14 #Python
Python中实例化class的执行顺序示例详解
Oct 14 #Python
Python读取Excel表格,并同时画折线图和柱状图的方法
Oct 14 #Python
对python3中pathlib库的Path类的使用详解
Oct 14 #Python
You might like
实用函数3
2007/11/08 PHP
深入浅出php socket编程
2015/05/13 PHP
php实现的数字验证码及数字运算验证码
2015/07/30 PHP
PHP结合Jquery和ajax实现瀑布流特效
2016/01/07 PHP
非常有用的9个PHP代码片段
2016/04/06 PHP
使用XHProf查找PHP性能瓶颈的实例
2017/12/13 PHP
使用jQuery简化Ajax开发 Ajax开发入门
2009/10/14 Javascript
firefox下input type="file"的size是多大
2011/10/24 Javascript
再探JavaScript作用域
2014/09/24 Javascript
jquery仿百度经验滑动切换浏览效果
2015/04/14 Javascript
js仿支付宝填写支付密码效果实现多方框输入密码
2016/03/09 Javascript
D3.js实现文本的换行详解
2016/10/14 Javascript
json数据处理及数据绑定
2017/01/25 Javascript
Bootstrap的popover(弹出框)2秒后定时消失的实现代码
2017/02/27 Javascript
node前端模板引擎Jade之标签的基本写法
2018/05/11 Javascript
原生javascript的ajax请求及后台PHP响应操作示例
2020/02/24 Javascript
Openlayers实现距离面积测量
2020/09/28 Javascript
[02:20]2014DOTA2西雅图邀请赛 MVP外卡赛首胜采访
2014/07/09 DOTA
[03:44]2014DOTA2国际邀请赛 71专访:DK战队赛前讨论视频遭泄露
2014/07/13 DOTA
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
从django的中间件直接返回请求的方法
2018/05/30 Python
深入分析python中整型不会溢出问题
2018/06/18 Python
python 正确保留多位小数的实例
2018/07/16 Python
python使用tornado实现简单爬虫
2018/07/28 Python
pymysql模块的操作实例
2019/12/17 Python
浅谈tensorflow之内存暴涨问题
2020/02/05 Python
学习Python列表的基础知识汇总
2020/03/10 Python
对python中return与yield的区别详解
2020/03/12 Python
Keras loss函数剖析
2020/07/06 Python
canvas实现滑动验证的实现示例
2020/08/11 HTML / CSS
澳大利亚家具和家居用品在线商店:Interiors Online
2018/03/05 全球购物
村容村貌整治方案
2014/05/21 职场文书
入党积极分子学习党的纲领思想汇报
2014/09/13 职场文书
大型主题婚礼活动策划方案
2014/09/15 职场文书
python字典的元素访问实例详解
2021/07/21 Python
python中对列表的删除和添加方法详解
2022/02/24 Python