用Python编写简单的定时器的方法


Posted in Python onMay 02, 2015

下面介绍以threading模块来实现定时器的方法。

首先介绍一个最简单实现:

import threading

def say_sth(str):
  print str
  t = threading.Timer(2.0, say_sth,[str])
  t.start()

if __name__ == '__main__':
  timer = threading.Timer(2.0,say_sth,['i am here too.'])
  timer.start()

不清楚在某些特殊应用场景下有什么缺陷否。

下面是所要介绍的定时器类的实现:

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 相关文章推荐
Python利用Beautiful Soup模块创建对象详解
Mar 27 Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
Feb 11 Python
Django Sitemap 站点地图的实现方法
Apr 29 Python
Python一行代码实现快速排序的方法
Apr 30 Python
Python多线程threading模块用法实例分析
May 22 Python
python的pytest框架之命令行参数详解(下)
Jun 27 Python
pandas取出重复数据的方法
Jul 04 Python
python绘制随机网络图形示例
Nov 21 Python
django连接mysql数据库及建表操作实例详解
Dec 10 Python
python实现跨excel sheet复制代码实例
Mar 03 Python
python pandas.DataFrame.loc函数使用详解
Mar 26 Python
Python confluent kafka客户端配置kerberos认证流程详解
Oct 12 Python
用Python程序抓取网页的HTML信息的一个小实例
May 02 #Python
在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
May 02 #Python
在Python的Django框架中用流响应生成CSV文件的教程
May 02 #Python
详细解读Python中的__init__()方法
May 02 #Python
举例讲解Python的Tornado框架实现数据可视化的教程
May 02 #Python
Python的Bottle框架中返回静态文件和JSON对象的方法
Apr 30 #Python
使用Python编写提取日志中的中文的脚本的方法
Apr 30 #Python
You might like
PHP 各种排序算法实现代码
2009/08/20 PHP
解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思
2013/06/28 PHP
[原创]PHP实现SQL语句格式化功能的方法
2017/07/28 PHP
PHP实现的用户注册表单验证功能简单示例
2019/02/25 PHP
PHP 实现缩略图
2021/03/09 PHP
js滚动条多种样式,推荐
2007/02/05 Javascript
javascript 动态添加事件代码
2008/11/30 Javascript
JQuery的一些小应用收集
2010/03/27 Javascript
精通Javascript系列之数值计算
2011/06/07 Javascript
nullJavascript中创建对象的五种方法实例
2013/05/07 Javascript
javascript 判断字符串是否包含某字符串及indexOf使用示例
2013/10/18 Javascript
director.js实现前端路由使用实例
2015/02/03 Javascript
jquery实现页面虚拟键盘特效
2015/08/08 Javascript
js实现的早期滑动门菜单效果代码
2015/08/27 Javascript
jQuery Mobile开发中日期插件Mobiscroll使用说明
2016/03/02 Javascript
基于JS实现导航条flash导航条
2016/06/17 Javascript
BootStrap Table 分页后重新搜索问题的解决办法
2016/08/08 Javascript
简单实现js倒计时功能
2017/02/13 Javascript
bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题
2017/08/10 Javascript
面包屑导航详解
2017/12/07 Javascript
javascript用defineProperty实现简单的双向绑定方法
2020/04/03 Javascript
vue+elementUI实现简单日历功能
2020/09/24 Javascript
Vue 数据响应式相关总结
2021/01/28 Vue.js
使用Python构建Hopfield网络的教程
2015/04/14 Python
python中使用iterrows()对dataframe进行遍历的实例
2018/06/09 Python
python实现n个数中选出m个数的方法
2018/11/13 Python
python中yield的用法详解——最简单,最清晰的解释
2019/04/04 Python
python opencv图片编码为h264文件的实例
2019/12/12 Python
python合并多个excel文件的示例
2020/09/23 Python
python statsmodel的使用
2020/12/21 Python
Tech21美国/加拿大:英国NO.1防摔保护壳品牌
2018/01/20 全球购物
小学毕业典礼主持词
2014/03/27 职场文书
五年级学生评语
2014/04/22 职场文书
高中课程设置方案
2014/05/28 职场文书
2014国庆节幼儿园亲子活动方案
2014/09/16 职场文书
服务器间如何实现文件共享
2022/05/20 Servers