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

 更新时间:2015年03月16日 10:01:01   作者:chongq  
这篇文章主要介绍了python通过线程实现定时器timer的方法,涉及Python线程与定时器timer的使用技巧,需要的朋友可以参考下

本文实例讲述了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 pip安装的包放在哪个目录(site-packages目录的位置)

    python pip安装的包放在哪个目录(site-packages目录的位置)

    这篇文章主要介绍了python pip安装的包放在哪个目录(site-packages目录的位置),通常安装在python安装目录下的lib/site-packages目录下,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • Python2/3中urllib库的一些常见用法

    Python2/3中urllib库的一些常见用法

    Urllib库是Python中的一个功能强大、用于操作URL,并在做爬虫的时候经常要用到的库。下面这篇文章主要给大家介绍了关于Python2/3中urllib库的一些常见用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-12-12
  • Django的CVB实例详解

    Django的CVB实例详解

    在本篇文章小编给大家整理的是关于Django的CVB实例详解内容,有需要的朋友们可以跟着学习下。
    2020-02-02
  • 信号生成及DFT的python实现方式

    信号生成及DFT的python实现方式

    今天小编就为大家分享一篇信号生成及DFT的python实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python程序设计入门(5)类的使用简介

    Python程序设计入门(5)类的使用简介

    这篇文章主要介绍了Python类的使用,需要的朋友可以参考下
    2014-06-06
  • Python中Django框架下的staticfiles使用简介

    Python中Django框架下的staticfiles使用简介

    这篇文章主要介绍了Python中Django框架下的staticfiles使用简介,staticfiles是一个帮助Django管理静态资源的工具,需要的朋友可以参考下
    2015-05-05
  • 将Python项目打包成exe并附带下载功能的操作流程

    将Python项目打包成exe并附带下载功能的操作流程

    这篇文章主要为大家详细介绍了将Python项目打包成exe并附带下载功能的操作流程,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以了解下
    2023-12-12
  • 使用matplotlib绘制热图(heatmap)全过程

    使用matplotlib绘制热图(heatmap)全过程

    这篇文章主要介绍了使用matplotlib绘制热图(heatmap)全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • python基础之while循环、for循环详解及举例

    python基础之while循环、for循环详解及举例

    所谓循环结构就是程序中控制某条或某些指令重复执行的结构,下面这篇文章主要给大家介绍了关于python基础之while循环、for循环的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • pytorch 可视化feature map的示例代码

    pytorch 可视化feature map的示例代码

    今天小编就为大家分享一篇pytorch 可视化feature map的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论