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中List的sort方法指南
Sep 01 Python
python实现在windows下操作word的方法
Apr 28 Python
python使用wxpython开发简单记事本的方法
May 20 Python
python使用win32com库播放mp3文件的方法
May 30 Python
python版本的读写锁操作方法
Apr 25 Python
Python中序列的修改、散列与切片详解
Aug 27 Python
python绘制热力图heatmap
Mar 23 Python
python采集微信公众号文章
Dec 20 Python
使用 Python 玩转 GitHub 的贡献板(推荐)
Apr 04 Python
Django使用unittest模块进行单元测试过程解析
Aug 02 Python
Pycharm创建项目时如何自动添加头部信息
Nov 14 Python
Python中读取文件名中的数字的实例详解
Dec 25 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
Symfony页面的基本创建实例详解
2015/01/26 PHP
PHP读取txt文本文件并分页显示的方法
2015/03/11 PHP
php实现文章评论系统
2019/02/18 PHP
jquery 注意事项与常用语法小结
2010/06/07 Javascript
JQuery入门——用one()方法绑定事件处理函数(仅触发一次)
2013/02/05 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
jQuery怎么解析Json字符串(Json格式/Json对象)
2013/08/09 Javascript
jquery判断RadioButtonList和RadioButton中是否有选中项示例
2013/09/29 Javascript
js中Image对象以及对其预加载处理示例
2013/11/20 Javascript
JS连接SQL数据库与ACCESS数据库的方法实例
2013/11/21 Javascript
javascript常用代码段搜集
2014/12/04 Javascript
jQuery中ready事件用法实例
2015/01/19 Javascript
javascript正则表达式基础知识入门
2015/04/20 Javascript
JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
2015/09/15 Javascript
去除html代码里面的script正则方法
2016/05/19 Javascript
jQuery的事件预绑定
2016/12/05 Javascript
Javascript 实现计算器时间功能详解及实例(二)
2017/01/08 Javascript
基于JS实现bookstore静态页面的实例代码
2017/02/22 Javascript
Vue.js实现数据响应的方法
2018/08/13 Javascript
JS实现骰子3D旋转效果
2019/10/24 Javascript
Element的el-tree控件后台数据结构的生成以及方法的抽取
2020/03/05 Javascript
JavaScript实现矩形块大小任意缩放
2020/08/25 Javascript
Vue路由 重定向和别名的区别说明
2020/09/09 Javascript
python输入错误密码用户锁定实现方法
2017/11/27 Python
分数霸榜! python助你微信跳一跳拿高分
2018/01/08 Python
django model通过字典更新数据实例
2020/04/01 Python
python中如何打包用户自定义模块
2020/09/23 Python
英国在线花园中心:You Garden
2018/06/03 全球购物
俄罗斯购买自行车网站:Vamvelosiped
2021/01/29 全球购物
药剂学专业应届生自荐信
2013/09/29 职场文书
酒店服务与管理毕业生求职信
2013/11/02 职场文书
应届大学生求职信
2013/12/01 职场文书
大学生见习报告范文
2014/11/03 职场文书
课堂打架检讨书200字
2014/11/21 职场文书
敬老院义诊活动总结
2015/05/07 职场文书
mybatis 获取无数据的字段不显示的问题
2021/07/15 Java/Android