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读取图片EXIF信息类库介绍和使用实例
Jul 10 Python
python编写爬虫小程序
May 14 Python
使用Python解析JSON数据的基本方法
Oct 15 Python
Python处理json字符串转化为字典的简单实现
Jul 07 Python
Python中enumerate函数代码解析
Oct 31 Python
解决Python安装后pip不能用的问题
Jun 12 Python
Python功能点实现:函数级/代码块级计时器
Jan 02 Python
初次部署django+gunicorn+nginx的方法步骤
Sep 11 Python
python pptx复制指定页的ppt教程
Feb 14 Python
Selenium向iframe富文本框输入内容过程图解
Apr 10 Python
python的reverse函数翻转结果为None的问题
May 11 Python
Django 解决由save方法引发的错误
May 21 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
国产动画《伍六七》原声大碟大卖,啊哈娱乐引领音乐赋能IP的新尝试
2020/03/08 国漫
PHP strtotime函数详解
2009/12/18 PHP
thinkPHP5框架实现基于ajax的分页功能示例
2018/06/12 PHP
PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
2019/02/15 PHP
Javascript 不能释放内存.
2006/09/07 Javascript
input 高级限制级用法
2009/03/26 Javascript
基于jquery的无缝循环新闻列表插件
2011/03/07 Javascript
使用js操作cookie的一点小收获分享
2013/09/03 Javascript
jquery 通过name快速取值示例
2014/01/24 Javascript
jquery实现鼠标拖拽滑动效果来选择数字的方法
2015/05/04 Javascript
jQuery简单实现tab选项卡切换效果
2016/06/20 Javascript
jQuery AJAX timeout 超时问题详解
2016/06/21 Javascript
web 前端常用组件之Layer弹出层组件
2016/09/22 Javascript
bootstrap读书笔记之CSS组件(上)
2016/10/17 Javascript
Axios学习笔记之使用方法教程
2017/07/21 Javascript
基于js的变量提升和函数提升(详解)
2017/09/17 Javascript
前端常见跨域解决方案(全)
2017/09/19 Javascript
微信小程序实现文字从右向左无限滚动
2020/11/18 Javascript
Javascript Web Worker使用过程解析
2020/03/16 Javascript
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
2021/01/29 jQuery
[42:52]Optic vs Serenity 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
介绍Python中几个常用的类方法
2015/04/08 Python
梅尔倒谱系数(MFCC)实现
2019/06/19 Python
python实现串口自动触发工作的示例
2019/07/02 Python
Python描述数据结构学习之哈夫曼树篇
2020/09/07 Python
Python 虚拟环境工作原理解析
2020/12/24 Python
IdealFit官方网站:女性蛋白质、补充剂和运动服装
2019/03/24 全球购物
Linux Interview Questions For software testers
2012/06/02 面试题
数学与统计学院学生个人职业生涯规划书
2014/02/10 职场文书
员工辞职信范文
2015/03/02 职场文书
2015年仓管员工作总结
2015/04/21 职场文书
超搞笑婚前保证书
2015/05/08 职场文书
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
2021/04/06 Oracle
Requests什么的通通爬不了的Python超强反爬虫方案!
2021/05/20 Python
Redis源码阅读:Redis字符串SDS详解
2021/07/15 Redis
HTML+CSS实现导航条下拉菜单的示例代码
2021/08/02 HTML / CSS