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装饰器使用方法实例
Nov 21 Python
使用Python实现一个简单的项目监控
Mar 31 Python
python实现根据用户输入从电影网站获取影片信息的方法
Apr 07 Python
Python基于回溯法子集树模板解决全排列问题示例
Sep 07 Python
python基本语法练习实例
Sep 19 Python
利用Python实现在同一网络中的本地文件共享方法
Jun 04 Python
Python 正则表达式匹配字符串中的http链接方法
Dec 25 Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
使用 Python 遍历目录树的方法
Feb 29 Python
opencv-python图像配准(匹配和叠加)的实现
Jun 23 Python
Python卷积神经网络图片分类框架详解分析
Nov 07 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 智能404跳转代码,适合换域名没改变目录的网站
2010/06/04 PHP
PHP中的一些常用函数收集
2015/05/26 PHP
php array_keys 返回数组的键名
2016/10/25 PHP
浅谈PHP发送HTTP请求的几种方式
2017/07/25 PHP
Windows Live的@live.com域名注册漏洞 利用代码
2006/12/27 Javascript
很全的显示阴历(农历)日期的js代码
2009/01/01 Javascript
JS下高效拼装字符串的几种方法比较与测试代码
2010/04/15 Javascript
JavaScript具有类似Lambda表达式编程能力的代码(改进版)
2010/09/14 Javascript
Jquery1.9.1源码分析系列(十五)动画处理之外篇
2015/12/04 Javascript
js表单中选择框值的获取及表单的序列化
2015/12/17 Javascript
微信小程序 下拉菜单的实现
2017/04/06 Javascript
微信小程序 五星评分的实现实例
2017/08/04 Javascript
浅谈SpringMVC中post checkbox 多选框value的值(隐藏域方式)
2018/01/08 Javascript
vue 实现数字滚动增加效果的实例代码
2018/07/06 Javascript
javascript json对象小技巧之键名作为变量用法分析
2019/11/11 Javascript
Vue filter 过滤当前时间 实现实时更新效果
2019/12/20 Javascript
javascript实现简易数码时钟
2020/03/30 Javascript
通过vue刷新左侧菜单栏操作
2020/08/06 Javascript
[01:38]完美世界DOTA2联赛PWL S3 集锦第四期
2020/12/21 DOTA
一个基于flask的web应用诞生 flask和mysql相连(4)
2017/04/11 Python
PyCharm在win10的64位系统安装实例
2017/11/26 Python
Django实现支付宝付款和微信支付的示例代码
2018/07/25 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
解决Keras 中加入lambda层无法正常载入模型问题
2020/06/16 Python
墨西哥网上购物:Linio墨西哥
2016/10/20 全球购物
美国存储和组织商店:The Container Store
2017/08/16 全球购物
利物浦足球俱乐部官方网上商店:Liverpool FC Official Store
2018/01/13 全球购物
JD Sports瑞典:英国领先的运动时尚商店
2018/01/28 全球购物
李维斯牛仔裤荷兰官方网站:Levi’s NL
2020/08/23 全球购物
linux面试相关问题
2012/08/11 面试题
小学信息技术教学反思
2014/02/10 职场文书
婚庆司仪开场白
2015/05/29 职场文书
单位车辆管理制度
2015/08/05 职场文书
2016党风廉政建设心得体会范文
2016/01/25 职场文书
《水浒传》读后感3篇(范文)
2019/09/19 职场文书
微信小程序 根据不同用户切换不同TabBar
2022/04/21 Javascript