python通过线程实现定时器timer的方法


Posted in Python onMarch 16, 2015

本文实例讲述了python通过线程实现定时器timer的方法。分享给大家供大家参考。具体分析如下:

这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数

下面介绍以threading模块来实现定时器的方法。

使用前先做一个简单试验:

import threading
def sayhello():
    print "hello world"
    global t    #Notice: use global variable!
    t = threading.Timer(5.0, sayhello)
    t.start()
t = threading.Timer(5.0, sayhello)
t.start()

运行结果如下:

>python hello.py
hello world
hello world
hello world

下面是定时器类的实现:

class Timer(threading.Thread):
    """
    very simple but useless timer.
    """
    def __init__(self, seconds):
        self.runTime = seconds
        threading.Thread.__init__(self)
    def run(self):
        time.sleep(self.runTime)
        print "Buzzzz!! Time's up!"
class CountDownTimer(Timer):
    """
    a timer that can counts down the seconds.
    """
    def run(self):
        counter = self.runTime
        for sec in range(self.runTime):
            print counter
            time.sleep(1.0)
            counter -= 1
        print "Done"
class CountDownExec(CountDownTimer):
    """
    a timer that execute an action at the end of the timer run.
    """
    def __init__(self, seconds, action, args=[]):
        self.args = args
        self.action = action
        CountDownTimer.__init__(self, seconds)
    def run(self):
        CountDownTimer.run(self)
        self.action(self.args)
def myAction(args=[]):
    print "Performing my action with args:"
    print args
if __name__ == "__main__":
    t = CountDownExec(3, myAction, ["hello", "world"])
    t.start()

以上代码在Python 2.5.4中运行通过

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Windows上使用virtualenv搭建Python+Flask开发环境
Jun 07 Python
Django自定义过滤器定义与用法示例
Mar 22 Python
python实现旋转和水平翻转的方法
Oct 25 Python
Python 3.6 -win64环境安装PIL模块的教程
Jun 20 Python
Python 获取指定文件夹下的目录和文件的实现
Aug 30 Python
Python 获取项目根路径的代码
Sep 27 Python
python中@property和property函数常见使用方法示例
Oct 21 Python
Python如何操作office实现自动化及win32com.client的运用
Apr 01 Python
Python用5行代码实现批量抠图的示例代码
Apr 14 Python
python实现画图工具
Aug 27 Python
浅析python字符串前加r、f、u、l 的区别
Jan 24 Python
Python pyecharts案例超市4年数据可视化分析
Aug 14 Python
python每隔N秒运行指定函数的方法
Mar 16 #Python
python实现登陆知乎获得个人收藏并保存为word文件
Mar 16 #Python
Python标准库urllib2的一些使用细节总结
Mar 16 #Python
python实现查询苹果手机维修进度
Mar 16 #Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 #Python
python实现简单的计时器功能函数
Mar 14 #Python
python将图片文件转换成base64编码的方法
Mar 14 #Python
You might like
解析ajax事件的调用顺序
2013/06/17 PHP
Laravel中log无法写入问题的解决
2017/06/17 PHP
php支付宝系列之电脑网站支付
2018/05/30 PHP
Laravel+Intervention实现上传图片功能示例
2019/07/09 PHP
Laravel如何同时连接多个数据库详解
2019/08/13 PHP
jquery 实现表单验证功能代码(简洁)
2012/07/03 Javascript
基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
2013/05/07 Javascript
如何使用Javascript正则表达式来格式化XML内容
2013/07/04 Javascript
js脚本获取webform服务器控件的方法
2014/05/16 Javascript
js实现iframe自动自适应高度的方法
2015/02/17 Javascript
详解JavaScript中数组的相关知识
2015/07/29 Javascript
JS实现的不规则TAB选项卡效果代码
2015/09/18 Javascript
Angular实现点击按钮控制隐藏和显示功能示例
2017/12/29 Javascript
VUE解决微信签名及SPA微信invalid signature问题(完美处理)
2019/03/29 Javascript
Vue+Django项目部署详解
2019/05/30 Javascript
vue中使用[provide/inject]实现页面reload的方法
2019/09/30 Javascript
Python性能优化的20条建议
2014/10/25 Python
python如何生成网页验证码
2018/07/28 Python
Python和Sublime整合过程图示
2019/12/25 Python
Python制作简易版小工具之计算天数的实现思路
2020/02/13 Python
python实点云分割k-means(sklearn)详解
2020/05/28 Python
HTML5中的强制下载属性download使用实例解析
2016/05/12 HTML / CSS
自动化专业本科毕业生求职信
2013/10/20 职场文书
中文专业毕业生自荐信
2013/10/28 职场文书
初中高效课堂实施方案
2014/02/26 职场文书
代办委托书怎样写
2014/04/08 职场文书
学校学雷锋活动总结
2014/06/26 职场文书
老干部工作先进事迹
2014/08/17 职场文书
2014年管理工作总结
2014/11/22 职场文书
综合测评个人总结
2015/03/03 职场文书
2015年结对帮扶工作总结
2015/05/04 职场文书
劳动仲裁调解书
2015/05/20 职场文书
电影地道战观后感
2015/06/04 职场文书
解析:创业计划书和商业计划书二者之间到底有什么区别
2019/08/14 职场文书
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
2022/04/04 Python
Golang 并发编程 SingleFlight模式
2022/04/26 Golang