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中使用动态变量名的方法
May 06 Python
Django中URLconf和include()的协同工作方法
Jul 20 Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 Python
使用Python创建简单的HTTP服务器的方法步骤
Apr 26 Python
详解Python的三种可变参数
May 08 Python
python求加权平均值的实例(附纯python写法)
Aug 22 Python
Python3.5 win10环境下导入kera/tensorflow报错的解决方法
Dec 19 Python
python+opencv3生成一个自定义纯色图教程
Feb 19 Python
jenkins+python自动化测试持续集成教程
May 12 Python
Python局部变量与全局变量区别原理解析
Jul 14 Python
解决pycharm 格式报错tabs和space不一致问题
Feb 26 Python
python中如何对多变量连续赋值
Jun 03 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 判断变量类型实现代码
2009/10/23 PHP
php设计模式之单例、多例设计模式的应用分析
2013/06/30 PHP
php从数据库中读取特定的行(实例)
2017/06/02 PHP
JavaScript 滚轮事件使用说明
2010/03/07 Javascript
在浏览器中获取当前执行的脚本文件名的代码
2011/07/19 Javascript
jQuery获取样式中的背景颜色属性值/颜色值
2012/12/17 Javascript
javascript html5摇一摇功能的实现
2016/04/19 Javascript
基于jQuery实现一个marquee无缝滚动的插件
2017/03/09 Javascript
jQuery插件ContextMenu自定义图标
2017/03/15 Javascript
JS实现读取xml内容并输出到div中的方法示例
2018/04/19 Javascript
Javascript获取某个月的天数
2018/05/30 Javascript
微信小程序手机号码验证功能的实例代码
2018/08/28 Javascript
玩转vue的slot内容分发
2018/09/22 Javascript
vue使用rem实现 移动端屏幕适配
2018/09/26 Javascript
javascript中call()、apply()的区别
2019/03/21 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
nodejs中各种加密算法的实现详解
2019/07/11 NodeJs
JS删除对象中某一属性案例详解
2020/09/08 Javascript
[57:18]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#3VP VS VG
2016/03/03 DOTA
[50:58]2018DOTA2亚洲邀请赛3月29日 小组赛A组OpTic VS Newbee
2018/03/30 DOTA
使用Python的web.py框架实现类似Django的ORM查询的教程
2015/05/02 Python
bpython 功能强大的Python shell
2016/02/16 Python
在Linux命令行终端中使用python的简单方法(推荐)
2017/01/23 Python
Django使用Mysql数据库已经存在的数据表方法
2018/05/27 Python
python检测主机的连通性并记录到文件的实例
2018/06/21 Python
python使用Paramiko模块实现远程文件拷贝
2019/04/30 Python
Python数据类型之Number数字操作实例详解
2019/05/08 Python
基于python二叉树的构造和打印例子
2019/08/09 Python
Python 生成VOC格式的标签实例
2020/03/10 Python
致跳远、跳高运动员广播稿
2014/01/09 职场文书
中学生校园广播稿
2014/01/16 职场文书
经营理念口号
2014/06/21 职场文书
班级读书活动总结
2014/06/30 职场文书
2016年重阳节慰问信
2015/12/01 职场文书
nginx中proxy_pass各种用法详解
2021/11/07 Servers
Python可视化神器pyecharts绘制地理图表
2022/07/07 Python