• 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中使用模块的教程
  • 编写Python脚本来实现最简单的FTP下载的教程
  • Python获取Windows或Linux主机名称通用函数分享
  • python 多线程应用介绍
  • Python警察与小偷的实现之一客户端与服务端通信实例
  • python实现的登录和操作开心网脚本分享
  • python导入时小括号大作用
  • 用Python编写一个基于终端的实现翻译的脚本
  • Python 中 list 的各项操作技巧
  • python 查找文件夹下所有文件 实现代码

文章分类

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

最近更新的内容

    • Python中shutil模块的学习笔记教程
    • python复制与引用用法分析
    • 详解Python中的装饰器、闭包和functools的教程
    • Python3基础之list列表实例解析
    • 详解Python的Django框架中的模版相关知识
    • 利用numpy+matplotlib绘图的基本操作教程
    • python实现bitmap数据结构详解
    • Python中urllib2模块的8个使用细节分享
    • python基于windows平台锁定键盘输入的方法
    • Python 执行字符串表达式函数(eval exec execfile)

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

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