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 相关文章推荐
pytyon 带有重复的全排列
Aug 13 Python
从零学Python之入门(三)序列
May 25 Python
Python的Django框架中settings文件的部署建议
May 30 Python
python生成器表达式和列表解析
Mar 10 Python
Python 爬虫多线程详解及实例代码
Oct 08 Python
Python正则表达式经典入门教程
May 22 Python
python遍历文件夹下所有excel文件
Jan 03 Python
详解python常用命令行选项与环境变量
Feb 20 Python
python实现梯度法 python最速下降法
Mar 24 Python
python3实现名片管理系统(控制台版)
Nov 29 Python
Python3 使用pip安装git并获取Yahoo金融数据的操作
Apr 08 Python
matplotlib之pyplot模块实现添加子图subplot的使用
Apr 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
php 动态执行带有参数的类方法
2009/04/10 PHP
基于HBase Thrift接口的一些使用问题及相关注意事项的详解
2013/06/03 PHP
在CentOS上搭建LAMP+vsftpd环境的简单指南
2015/08/01 PHP
PHP使用Pthread实现的多线程操作实例
2015/11/14 PHP
PHP获取链表中倒数第K个节点的方法
2018/01/18 PHP
thinkPHP5框架分页样式类完整示例
2018/09/01 PHP
PHP利用DWZ.CN服务生成短网址
2019/08/11 PHP
Laravel5.3+框架定义API路径取消CSRF保护方法详解
2020/04/06 PHP
js中的eventType事件及其浏览器支持性介绍
2013/11/29 Javascript
js如何实现点击标签文字,文字在文本框出现
2015/08/05 Javascript
总结JavaScript设计模式编程中的享元模式使用
2016/05/21 Javascript
jQuery使用getJSON方法获取json数据完整示例
2016/09/13 Javascript
nodejs开发微信小程序实现密码加密
2017/07/11 NodeJs
jQuery实现的简单前端搜索功能示例
2017/10/28 jQuery
在vue中添加Echarts图表的基本使用教程
2017/11/22 Javascript
使用vue完成微信公众号网页小记(推荐)
2019/04/28 Javascript
Vue分页效果与购物车功能
2019/12/13 Javascript
2019年度web前端面试题总结(主要为Vue面试题)
2020/01/12 Javascript
Vue循环遍历选项赋值到对应控件的实现方法
2020/06/22 Javascript
小程序自动化测试的示例代码
2020/08/11 Javascript
javascript实现固定侧边栏
2021/02/09 Javascript
[00:35]可解锁地面特效
2018/12/20 DOTA
wxPython中文教程入门实例
2014/06/09 Python
优化Python代码使其加快作用域内的查找
2015/03/30 Python
Python3.2中Print函数用法实例详解
2015/05/19 Python
python的pdb调试命令的命令整理及实例
2017/07/12 Python
python实现手机通讯录搜索功能
2018/02/22 Python
对Python 简单串口收发GUI界面的实例详解
2019/06/12 Python
python自动下载图片的方法示例
2020/03/25 Python
开普敦通行证:Cape Town Pass
2019/07/18 全球购物
华为消费者德国官方网站:HUAWEI德国
2020/11/03 全球购物
弘扬职业精神演讲稿
2014/03/20 职场文书
婚前协议书标准版
2014/10/19 职场文书
乡镇法制宣传日活动总结
2015/05/05 职场文书
go语言求任意类型切片的长度操作
2021/04/26 Golang
Sleuth+logback 设置traceid 及自定义信息方式
2021/07/26 Java/Android