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 相关文章推荐
python中函数传参详解
Jul 03 Python
Python实现中一次读取多个值的方法
Apr 22 Python
python调用百度语音REST API
Aug 30 Python
python开发之anaconda以及win7下安装gensim的方法
Jul 05 Python
Python使用tkinter模块实现推箱子游戏
Oct 08 Python
Python小程序之在图片上加入数字的代码
Nov 26 Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
Jan 20 Python
Python restful框架接口开发实现
Apr 13 Python
python实现程序重启和系统重启方式
Apr 16 Python
TensorFlow保存TensorBoard图像操作
Jun 23 Python
python实现简单遗传算法
Sep 18 Python
Python爬虫爬取有道实现翻译功能
Nov 27 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
PHP 杂谈《重构-改善既有代码的设计》之五 简化函数调用
2012/05/07 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
2014/08/15 PHP
ThinkPHP文件上传实例教程
2014/08/22 PHP
PHP关联数组实现根据元素值删除元素的方法
2015/06/26 PHP
PHP实现的oracle分页函数实例
2016/01/25 PHP
Jquery 1.42 checkbox 全选和反选代码
2010/03/27 Javascript
利用jQuery实现可输入搜索文字的下拉框
2013/10/23 Javascript
js获取IFRAME当前的URL的方法
2013/11/13 Javascript
做好七件事帮你提升jQuery的性能
2014/02/06 Javascript
Jquery全屏相册插件zoomvisualizer具有调节放大与缩小功能
2015/11/02 Javascript
微信小程序 form组件详解及简单实例
2017/01/10 Javascript
JS实现禁止用户使用Ctrl+鼠标滚轮缩放网页的方法
2017/04/28 Javascript
浅析JS抽象工厂模式
2017/12/14 Javascript
webpack里使用jquery.mCustomScrollbar插件的方法
2018/05/30 jQuery
微信小程序的线程架构【推荐】
2019/05/14 Javascript
JAVA面试题 static关键字详解
2019/07/16 Javascript
采用Psyco实现python执行速度提高到与编译语言一样的水平
2014/10/11 Python
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
2014/11/05 Python
Python之时间和日期使用小结
2019/02/14 Python
python使用PyQt5的简单方法
2019/02/27 Python
Python Flask框架模板操作实例分析
2019/05/03 Python
Python中私有属性的定义方式
2020/03/05 Python
pyinstaller打包找不到文件的问题解决
2020/04/15 Python
pyCharm 实现关闭代码检查
2020/06/09 Python
Python环境管理virtualenv&virtualenvwrapper的配置详解
2020/07/01 Python
css3中的calc函数浅析
2018/07/10 HTML / CSS
用HTML5实现手机摇一摇的功能的教程
2012/10/30 HTML / CSS
Wiggle中国:英国骑行、跑步、游泳 & 铁三运动装备专卖网店
2016/08/02 全球购物
测试工程师岗位职责
2013/11/28 职场文书
品质主管的岗位职责
2013/12/04 职场文书
党章学习思想汇报
2014/01/14 职场文书
单位实习证明怎么写
2014/01/17 职场文书
内刊编辑求职自荐书范文
2014/02/19 职场文书
2014年社会实践活动总结范文
2014/04/29 职场文书
2014购房个人委托书范本
2014/10/12 职场文书
三严三实学习心得体会(精选N篇)
2016/01/05 职场文书