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用zip函数同时遍历多个迭代器示例详解
Nov 14 Python
python2.7 mayavi 安装图文教程(推荐)
Jun 22 Python
Python 操作文件的基本方法总结
Aug 10 Python
Python基于正则表达式实现文件内容替换的方法
Aug 30 Python
python绘制铅球的运行轨迹代码分享
Nov 14 Python
Python中elasticsearch插入和更新数据的实现方法
Apr 01 Python
numpy使用fromstring创建矩阵的实例
Jun 15 Python
python之super的使用小结
Aug 13 Python
Python 新建文件夹与复制文件夹内所有内容的方法
Oct 27 Python
在Python中关于使用os模块遍历目录的实现方法
Jan 03 Python
学会Python数据可视化必须尝试这7个库
Jun 16 Python
浅谈Python数学建模之整数规划
Jun 23 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中将网址转换为超链接的函数
2011/09/02 PHP
用PHP和Shell写Hadoop的MapReduce程序
2014/04/15 PHP
通过curl模拟post和get方式提交的表单类
2014/04/23 PHP
使用URL传输SESSION信息
2015/07/14 PHP
PHP对象相关知识总结
2017/04/09 PHP
PHP中localeconv()函数的用法
2019/03/26 PHP
js生成随机数之random函数随机示例
2013/12/20 Javascript
JS打字效果的动态菜单代码分享
2015/08/21 Javascript
JS验证邮件地址格式方法小结
2015/12/01 Javascript
Bootstrap前端开发案例一
2016/06/17 Javascript
JS实现数字格式千分位相互转换方法
2016/08/01 Javascript
JavaScript使用readAsDataUrl方法预览图片
2017/05/10 Javascript
bootstrap时间控件daterangepicker使用方法及各种小bug修复
2017/10/25 Javascript
JS对日期操作封装代码实例
2019/11/08 Javascript
Vue.js桌面端自定义滚动条组件之美化滚动条VScroll
2020/12/01 Vue.js
详解appium+python 启动一个app步骤
2017/12/20 Python
python+selenium实现登录账户后自动点击的示例
2017/12/22 Python
对python中raw_input()和input()的用法详解
2018/04/22 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
用python写一个定时提醒程序的实现代码
2019/07/22 Python
Python封装成可带参数的EXE安装包实例
2019/08/24 Python
Windows10下 python3.7 安装 facenet的教程
2019/09/10 Python
Python3实现将一维数组按标准长度分隔为二维数组
2019/11/29 Python
Django vue前后端分离整合过程解析
2020/11/20 Python
Django URL参数Template反向解析
2020/11/24 Python
python实现企业微信定时发送文本消息的示例代码
2020/11/24 Python
video下autoplay属性无效的解决方法(添加muted属性)
2020/05/19 HTML / CSS
全球最大运动品牌的男装、女装和童装官方库存商:A&A Sports
2021/01/17 全球购物
融资合作协议书范本
2014/10/17 职场文书
大连星海广场导游词
2015/02/10 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
2016中秋节月饼促销广告语
2016/01/28 职场文书
Oracle 数据仓库ETL技术之多表插入语句的示例详解
2021/04/12 Oracle
golang 在windows中设置环境变量的操作
2021/04/29 Golang
python 解决微分方程的操作(数值解法)
2021/05/26 Python
Pandas 稀疏数据结构的实现
2021/07/25 Python