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使用线程封装的一个简单定时器类实例
本文实例讲述了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编写简单的定时器的方法
下面介绍以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() 不清楚在某些特殊应用场景下有什么缺陷
-
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定时器实例代码
在实际应用中,我们经常需要使用定时器去触发一些事件.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
-
wxPython定时器wx.Timer简单应用实例
本文实例讲述了wxPython定时器wx.Timer简单应用.分享给大家供大家参考.具体如下: # -*- coding: utf-8 -*- ######################################################## ## 这是wxPython定时器wx.Timer的简单应用 ## testwxTimer1.pyw ######################################################## import wx impo
-
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实现定时精度可调节的定时器,供大家参考,具体内容如下 # -* 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
-
python通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
随机推荐
- jquery给图片添加鼠标经过时的边框效果
- oracle快速删除重复的记录
- js精美的幻灯片画集特效代码分享
- js 连接数据库如何操作数据库中的数据
- 使用JavaScript创建新样式表和新样式规则
- 使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
- PHP排序之二维数组的按照字母排序实现代码
- Python装饰器的函数式编程详解
- python将人民币转换大写的脚本代码
- MySQL5.6 数据库主从同步安装与配置详解(Master/Slave)
- MySQL的使用中实现读写分离的教程
- 使用DNode实现php和nodejs之间通信的简单实例
- Windows和Linux中php代码调试工具Xdebug的安装与配置详解
- Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
- easyUI combobox实现联动效果
- 跟我学习JScript的Bug与内存管理
- js实现用滚动条来放大缩小图片的代码
- js监听鼠标事件控制textarea输入字符串的个数
- iOS开发之适配iOS10以及Xcode8
- php auth_http类库进行身份效验