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中拼接路径os.path.join斜杠的问题
Oct 23 Python
pyttsx3实现中文文字转语音的方法
Dec 24 Python
python实现播放音频和录音功能示例代码
Dec 30 Python
使用python Telnet远程登录执行程序的方法
Jan 26 Python
python装饰器简介---这一篇也许就够了(推荐)
Apr 01 Python
python实现KNN分类算法
Oct 16 Python
python装饰器的特性原理详解
Dec 25 Python
pandas中ix的使用详细讲解
Mar 09 Python
pytorch中的weight-initilzation用法
Jun 24 Python
如何在Win10系统使用Python3连接Hive
Oct 15 Python
PyTorch 实现L2正则化以及Dropout的操作
May 27 Python
Python中 range | np.arange | np.linspace三者的区别
Mar 22 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
laravel 5 实现模板主题功能
2015/03/02 PHP
php结合正则批量抓取网页中邮箱地址
2015/05/19 PHP
php版微信公众平台接口参数调试实现判断用户行为的方法
2016/09/23 PHP
Mootools 1.2教程(2) DOM选择器
2009/09/14 Javascript
纯js实现瀑布流展现照片(自动适应窗口大小)
2013/04/08 Javascript
Node.js中对通用模块的封装方法
2014/06/06 Javascript
纯js实现div内图片自适应大小(已测试,兼容火狐)
2014/06/16 Javascript
js获取UserControl内容为拼html时提供方便
2014/11/02 Javascript
让JavaScript中setTimeout支持链式操作的方法
2015/06/19 Javascript
Javascript的比较汇总
2016/07/25 Javascript
request请求获取参数的实现方法(post和get两种方式)
2016/09/27 Javascript
关于js函数解释(包括内嵌,对象等)
2016/11/20 Javascript
详解javascript表单的Ajax提交插件的使用
2016/12/29 Javascript
JS解决IOS中拍照图片预览旋转90度BUG的问题
2017/09/13 Javascript
JS运动特效之同时运动实现方法分析
2018/01/24 Javascript
vue项目中mock.js的使用及基本用法
2019/05/22 Javascript
vue插槽slot的简单理解与用法实例分析
2020/03/14 Javascript
[57:55]完美世界DOTA2联赛PWL S3 Magma vs Phoenix 第二场 12.12
2020/12/16 DOTA
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
python实战之实现excel读取、统计、写入的示例讲解
2018/05/02 Python
Python计算一个给定时间点前一个月和后一个月第一天的方法
2018/05/29 Python
python使用scrapy发送post请求的坑
2018/09/04 Python
Python实现html转换为pdf报告(生成pdf报告)功能示例
2019/05/04 Python
对Keras中predict()方法和predict_classes()方法的区别说明
2020/06/09 Python
Python内存泄漏和内存溢出的解决方案
2020/09/26 Python
eBay德国站:eBay.de
2017/09/14 全球购物
BISSELL官网:北美吸尘器第一品牌
2019/03/14 全球购物
Juice Beauty官网:有机美容产品,护肤与化妆品
2020/06/13 全球购物
自荐信如何“自荐”
2013/10/24 职场文书
设计部经理的岗位职责
2013/11/16 职场文书
党员组织关系介绍信
2014/02/13 职场文书
优秀党员个人总结
2015/02/14 职场文书
2015年艾滋病宣传活动总结
2015/03/27 职场文书
公司处罚决定书
2015/06/24 职场文书
浅谈Nginx 中的两种限流方式
2021/03/31 Servers
Python可视化神器pyecharts之绘制地理图表练习
2022/07/07 Python