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

python通过线程实现定时器timer的方法

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

通过本文主要向大家介绍了python timer,python timer模块,c timer 多线程,timer 线程,java timer 线程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()
</div>

运行结果如下:

>python hello.py
hello world
hello world
hello world
</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>

以上代码在Python 2.5.4中运行通过

希望本文所述对大家的Python程序设计有所帮助。

</div>

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

  • python定时器(Timer)用法简单实例
  • Python定时执行之Timer用法示例
  • Python定时执行之Timer用法示例
  • python通过线程实现定时器timer的方法
  • python通过线程实现定时器timer的方法

相关文章

  • python开发之list操作实例分析
  • Python编程之属性和方法实例详解
  • python判断给定的字符串是否是有效日期的方法
  • 跟老齐学Python之总结参数的传递
  • Python操作MongoDB数据库PyMongo库使用方法
  • 初步理解Python进程的信号通讯
  • Django的数据模型访问多对多键值的方法
  • Python爬取京东的商品分类与链接
  • 总结用Pdb库调试Python的方式及常用的命令
  • python使用matplotlib绘制折线图教程

文章分类

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

最近更新的内容

    • Python中字典的基础知识归纳小结
    • Python 实现简单的电话本功能
    • python操作MySQL数据库的方法分享
    • 使用Python神器对付12306变态验证码
    • Python匹配中文的正则表达式
    • Python实现统计英文单词个数及字符串分割代码
    • python通过pil将图片转换成黑白效果的方法
    • python动态监控日志内容的示例
    • 用实例解释Python中的继承和多态的概念
    • python类继承与子类实例初始化用法分析

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

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