对python周期性定时器的示例详解


Posted in Python onFebruary 19, 2019

一、用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 
    self.function = function
    self.args = args
    self.kwargs = kwargs
    self.finished = threading.Event()

  def cancel(self):
    self.finished.set() 

  def run(self):
    self.finished.wait(self.interval) 
    if not self.finished.is_set():
      self.function(*self.args, **self.kwargs)
    self.finished.set()
    
class LoopTimer(_Timer):
  def __init__(self, interval, function, args=[], kwargs={}):
    _Timer.__init__(self, interval, function, args, kwargs)

  def run(self):
    while True:
      if not self.finished.is_set():
        self.finished.wait(self.interval)
        self.function(*self.args, **self.kwargs) 
      else:
        break


def testlooptimer():
  print("loop timer")


if __name__ == '__main__':
  t = LoopTimer(3.0,testlooptimer)
  t.start()

二、 使用

import py_timer

def serv_start():
#Perform first fork.
try:
      thread_timer = py_timer.LoopTimer(timeout, start_timer)
      thread_timer.start()
      thread_timer.cancel() #

    except Exception, ex:                            
      print("daemon: %s %s", type(ex), ex)



def start_timer():

print 'hello'

以上这篇对python周期性定时器的示例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
wxPython窗口中文乱码解决方法
Oct 11 Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 Python
解决Python字典写入文件出行首行有空格的问题
Sep 27 Python
简单谈谈Python的pycurl模块
Apr 07 Python
pygame游戏之旅 添加键盘按键的方法
Nov 20 Python
Python装饰器限制函数运行时间超时则退出执行
Apr 09 Python
Python 硬币兑换问题
Jul 29 Python
用django设置session过期时间的方法解析
Aug 05 Python
python之PyQt按钮右键菜单功能的实现代码
Aug 17 Python
django实现用户注册实例讲解
Oct 30 Python
基于Keras 循环训练模型跑数据时内存泄漏的解决方式
Jun 11 Python
聊聊python中的异常嵌套
Sep 01 Python
Python3数字求和的实例
Feb 19 #Python
对Python定时任务的启动和停止方法详解
Feb 19 #Python
python的schedule定时任务模块二次封装方法
Feb 19 #Python
Python3 max()函数基础用法
Feb 19 #Python
Python3 log10()函数简单用法
Feb 19 #Python
Python3中exp()函数用法分析
Feb 19 #Python
详解Python3中ceil()函数用法
Feb 19 #Python
You might like
SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享
2011/04/18 PHP
PHP引用符&的用法详细解析
2013/08/22 PHP
初识PHP
2014/09/28 PHP
值得分享的php+ajax实时聊天室
2016/07/20 PHP
功能强大的PHP发邮件类
2016/08/29 PHP
如何在centos8自定义目录安装php7.3
2019/11/28 PHP
PHP ElasticSearch做搜索实例讲解
2020/02/05 PHP
document.onreadystatechange事件的用法分析
2009/10/17 Javascript
Jquery Change与bind事件代码
2011/09/29 Javascript
jquery滚动组件(vticker.js)实现页面动态数据的滚动效果
2013/07/03 Javascript
基于JavaScript实现瀑布流效果(循环渐近)
2016/01/27 Javascript
vue脚手架vue-cli的学习使用教程
2017/06/06 Javascript
小程序自定义导航栏兼容适配所有机型(附完整案例)
2020/04/26 Javascript
[06:21]完美世界亚洲区首席发行官竺琦TI3采访
2013/08/26 DOTA
[06:43]DAC2018 4.5 SOLO赛 Maybe vs Paparazi
2018/04/06 DOTA
python连接远程ftp服务器并列出目录下文件的方法
2015/04/01 Python
python中的闭包函数
2018/02/09 Python
Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)
2018/04/05 Python
使用python实现http及ftp服务进行数据传输的方法
2018/10/26 Python
HTML的form表单和django的form表单
2019/07/25 Python
关于numpy中eye和identity的区别详解
2019/11/29 Python
Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】
2020/05/29 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
2020/06/01 Python
python中xlrd模块的使用详解
2021/02/01 Python
Django实现简单的分页功能
2021/02/22 Python
新员工欢迎词
2014/01/12 职场文书
鸿星尔克广告词
2014/03/21 职场文书
幼儿园评语大全
2014/04/17 职场文书
求职信内容怎么写
2014/05/26 职场文书
办公室主任个人对照检查材料思想汇报
2014/10/11 职场文书
2014年终个人总结报告
2015/03/09 职场文书
2016新年慰问信范文
2015/03/25 职场文书
升学宴家长致辞
2015/07/27 职场文书
聊聊SpringBoot自动装配的魔力
2021/11/17 Java/Android
SQL SERVER触发器详解
2022/02/24 SQL Server
Python tensorflow卷积神经Inception V3网络结构
2022/05/06 Python