用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中pass语句用法实例分析
Apr 30 Python
python基于pyDes库实现des加密的方法
Apr 29 Python
Python之Scrapy爬虫框架安装及使用详解
Nov 16 Python
Python通过Django实现用户注册和邮箱验证功能代码
Dec 11 Python
python3 破解 geetest(极验)的滑块验证码功能
Feb 24 Python
Python OpenCV获取视频的方法
Feb 28 Python
Python2实现的图片文本识别功能详解
Jul 11 Python
python3.7 sys模块的具体使用
Jul 22 Python
python pycharm的安装及其使用
Oct 11 Python
在jupyter notebook中调用.ipynb文件方式
Apr 14 Python
python爬虫如何解决图片验证码
Feb 14 Python
python实现剪贴板的操作
Jul 01 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几个数学计算的内部函数学习整理
2011/08/06 PHP
php实现把url转换迅雷thunder资源下载地址的方法
2014/11/07 PHP
php实现支付宝当面付(扫码支付)功能
2018/05/30 PHP
Jquery cookie操作代码
2010/03/14 Javascript
js中的preventDefault与stopPropagation详解
2014/01/29 Javascript
js中的getAttribute方法使用示例
2014/08/01 Javascript
js判断文本框剩余可输入字数的方法
2015/02/04 Javascript
jQuery选择器源码解读(一):Sizzle方法
2015/03/31 Javascript
浅谈js中的三种继承方式及其优缺点
2016/08/10 Javascript
JavaScript实现经典排序算法之插入排序
2016/12/28 Javascript
数组Array的排序sort方法
2017/02/17 Javascript
vue.js如何更改默认端口号8080为指定端口的方法
2017/07/14 Javascript
Vue+Element UI+Lumen实现通用表格分页功能
2019/02/02 Javascript
解决vue2中使用elementUi打包报错的问题
2020/09/22 Javascript
python实现多线程抓取知乎用户
2016/12/12 Python
django admin 后台实现三级联动的示例代码
2018/06/22 Python
pyqt 实现在Widgets中显示图片和文字的方法
2019/06/13 Python
Spring实战之使用util:命名空间简化配置操作示例
2019/12/09 Python
Python datetime 如何处理时区信息
2020/09/02 Python
瑞贝卡·泰勒官方网站:Rebecca Taylor
2016/09/24 全球购物
加热夹克:RAVEAN
2018/10/19 全球购物
奖学金自我鉴定范文
2013/10/03 职场文书
创业计划书六个要素
2013/12/26 职场文书
餐饮采购员岗位职责
2014/03/15 职场文书
陈安之励志演讲稿
2014/08/21 职场文书
社区党建工作汇报材料
2014/10/27 职场文书
中学生检讨书范文
2014/11/03 职场文书
2014年社区工会工作总结
2014/12/18 职场文书
工作检讨书范文
2015/01/23 职场文书
2015年前台个人工作总结
2015/04/03 职场文书
乡镇法制宣传日活动总结
2015/05/05 职场文书
2016简单的租房合同范本
2016/03/18 职场文书
.Net Core导入千万级数据至Mysql的步骤
2021/05/24 MySQL
Spring中的使用@Async异步调用方法
2021/11/01 Java/Android
java协程框架quasar和kotlin中的协程对比分析
2022/02/24 Java/Android
抖音动画片,皮皮虾,《治愈系》动画在用这首REMIX作为背景音乐,Anak ,The last world with you完整版
2022/03/16 杂记