• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 用Python编写简单的定时器的方法

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 定时器,python 定时任务,python 定时执行,python 定时,python 设置定时任务等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

下面介绍以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()

</div>

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

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

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() 
</div>

</div>

您可能想查找下面的文章:

  • Python实现定时任务
  • 用Python编写简单的定时器的方法
  • 用Python编写简单的定时器的方法

相关文章

  • Python的Django框架中模板碎片缓存简介
  • Python编写生成验证码的脚本的教程
  • 详解使用python crontab设置linux定时任务
  • 使用Python的Treq on Twisted来进行HTTP压力测试
  • 零基础写python爬虫之打包生成exe文件
  • TFRecord格式存储数据与队列读取实例
  • 在Python中使用Mako模版库的简单教程
  • Python Django使用forms来实现评论功能
  • 一则python3的简单爬虫代码
  • Python函数的周期性执行实现方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python基础教程之基本内置数据类型介绍
    • Python中的闭包详细介绍和实例
    • Python实现监控程序执行时间并将其写入日志的方法
    • python分割和拼接字符串
    • 分享一下Python 开发者节省时间的10个方法
    • Python实用日期时间处理方法汇总
    • 连接Python程序与MySQL的教程
    • Python数据分析之真实IP请求Pandas详解
    • Python 代码性能优化技巧分享
    • Python简单获取自身外网IP的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有