python 定时器,轮询定时器的实例
python 定时器默认定时器只执行一次,第一个参数单位S,几秒后执行
import threading def fun_timer(): print('Hello Timer!') timer = threading.Timer(1, fun_timer) timer.start()
改成以下可以执行多次
建立loop_timer.py
from threading import _Timer class LoopTimer(_Timer): """Call a function after a specified number of seconds: t = LoopTi mer(30.0, f, args=[], kwargs={}) t.start() t.cancel() # stop the timer's action if it's still waiting """ def __init__(self, interval, function, args=[], kwargs={}): _Timer.__init__(self, interval, function, args, kwargs) def run(self): '''self.finished.wait(self.interval) if not self.finished.is_set(): self.function(*self.args, **self.kwargs) self.finished.set()''' while True: self.finished.wait(self.interval) if self.finished.is_set(): self.finished.set() break self.function(*self.args, **self.kwargs)
调用
t = LoopTimer(120, fun_timer) t.start()
以上这篇python 定时器,轮询定时器的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
相关推荐
-
python定时器使用示例分享
复制代码 代码如下: class SLTimer(multiprocessing.Process): #from datetime import datetime #import time def __init__(self, target=None, args=(), kwargs={},date=None,time=None): '''\ @param date 1900-01-01 @param time 00:00:00
-
python使用线程封装的一个简单定时器类实例
本文实例讲述了python使用线程封装的一个简单定时器类.分享给大家供大家参考.具体实现方法如下: from threading import Timer class MyTimer: def __init__(self): self._timer= None self._tm = None self._fn = None def _do_func(self): if self._fn: self._fn() self._do_start() def _do_start(self): self.
-
python单线程实现多个定时器示例
单线程实现多个定时器 NewTimer.py 复制代码 代码如下: #!/usr/bin/env python from heapq import *from threading import Timerimport threadingimport uuidimport timeimport datetimeimport sysimport math global TimerStampglobal TimerTimes class CancelFail(Exception): pass c
-
Python定时器实例代码
在实际应用中,我们经常需要使用定时器去触发一些事件.Python中通过线程实现定时器timer,其使用非常简单.看示例: import threading def fun_timer(): print('Hello Timer!') timer = threading.Timer(1, fun_timer) timer.start() 输出结果: Hello Timer! Process finished with exit code 0 注意,只输出了一次,程序就结束了,显然不是我们想要的结果
-
python 定时器,实现每天凌晨3点执行的方法
如下所示: ''' Created on 2018-4-20 例子:每天凌晨3点执行func方法 ''' import datetime import threading def func(): print("haha") #如果需要循环调用,就要添加以下方法 timer = threading.Timer(86400, func) timer.start() # 获取现在时间 now_time = datetime.datetime.now() # 获取明天时间 next_time
-
Python实现定时精度可调节的定时器
本文实例为大家分享了Python实现定时精度可调节的定时器,供大家参考,具体内容如下 # -* coding: utf-8 -*- import sys import os import getopt import threading import time def Usage(): usage_str = '''''说明: \t定时器 \timer.py -h 显示本帮助信息,也可以使用--help选项 \timer.py -d num 指定一个延时时间(以毫秒为单位) \t 也可以使用--d
-
对python周期性定时器的示例详解
一.用thread实现定时器 py_timer.py文件 #!/usr/bin/python #coding:utf-8 import threading import os import sys class _Timer(threading.Thread): def __init__(self, interval, function, args=[], kwargs={}): threading.Thread.__init__(self) self.interval = interval se
-
wxPython定时器wx.Timer简单应用实例
本文实例讲述了wxPython定时器wx.Timer简单应用.分享给大家供大家参考.具体如下: # -*- coding: utf-8 -*- ######################################################## ## 这是wxPython定时器wx.Timer的简单应用 ## testwxTimer1.pyw ######################################################## import wx impo
-
python通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
-
python定时器(Timer)用法简单实例
本文实例讲述了python定时器(Timer)用法.分享给大家供大家参考.具体如下: # encoding: UTF-8 import threading #Timer(定时器)是Thread的派生类, #用于在指定时间后调用一个方法. def func(): print 'hello timer!' timer = threading.Timer(5, func) timer.start() 该程序可实现延迟5秒后调用func方法的功能. 希望本文所述对大家的Python程序设计有所帮助.
-
用Python编写简单的定时器的方法
下面介绍以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() 不清楚在某些特殊应用场景下有什么缺陷
随机推荐
- C# 正则表达式经典分类整理集合手册第1/3页
- 解析centos中Apache、php、mysql 默认安装路径
- SQL2000中的默认sa帐号的修改与删除方法
- cmd rd命令 删除文件夹
- 微信小程序实现皮肤功能(夜间模式)
- 深入密码加salt原理的分析
- 在IE下获取object(ActiveX)的Param的代码
- Struts2开发环境搭建 附简单登录功能实例
- 在SQL Server中实现最短路径搜索的解决方法
- javascript创建含数字字母的随机字符串方法总结
- actionscript与javascript的区别
- 浅谈C++对象的内存分布和虚函数表
- Java程序员转Android开发必读经验一份
- Android 自定义TextView实现文本内容自动调整字体大小
- C++空类详解
- PHP开发中csrf攻击的简单演示和防范
- C++中stack、queue、vector的用法详解
- flask使用session保存登录状态及拦截未登录请求代码
- 基于Vue和Element-Ui搭建项目的方法
- Oracle判断表、列、主键是否存在的方法