python自己做个定时器(对python周期性定时器的示例详解)
类别:脚本大全 浏览量:2329
时间:2022-01-25 00:56:51 python自己做个定时器
对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 self .function = function self .args = args self .kwargs = kwargs self .finished = threading.Event() def cancel( self ): self .finished. set () def run( self ): self .finished.wait( self .interval) if not self .finished.is_set(): self .function( * self .args, * * self .kwargs) self .finished. set () class LoopTimer(_Timer): def __init__( self , interval, function, args = [], kwargs = {}): _Timer.__init__( self , interval, function, args, kwargs) def run( self ): while True : if not self .finished.is_set(): self .finished.wait( self .interval) self .function( * self .args, * * self .kwargs) else : break def testlooptimer(): print ( "loop timer" ) if __name__ = = '__main__' : t = LoopTimer( 3.0 ,testlooptimer) t.start() |
二、 使用
|
import py_timer def serv_start(): #Perform first fork. try : thread_timer = py_timer.LoopTimer(timeout, start_timer) thread_timer.start() thread_timer.cancel() # except Exception, ex: print ( "daemon: %s %s" , type (ex), ex) def start_timer(): print 'hello' |
以上这篇对python周期性定时器的示例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/u013827488/article/details/74356795
您可能感兴趣
- python3字符串怎么操作(Python3字符串encode与decode的讲解)
- python使用telnet(python 处理telnet返回的More,以及get想要的那个参数方法)
- python端口扫描脚本测试(Python实现的IP端口扫描工具类示例)
- python正则表达式处理教学(使用Python正则表达式操作文本数据的方法)
- python自动识别旋转验证码(Python实现字符型图片验证码识别完整过程详解)
- python冒泡排序代码通俗理解(详解python算法之冒泡排序)
- python闭包的讲解(详解python函数的闭包问题内部函数与外部函数详述)
- python爬虫开源代码(Python实现的文轩网爬虫完整示例)
- pythonimage库(Python/Django后端使用PIL Image生成头像缩略图)
- python递归深度遍历多叉树(Python实现二叉树的常见遍历操作总结7种方法)
- python中匿名函数关键字(浅谈python之高阶函数和匿名函数)
- python对mysql数据分析(python使用adbapi实现MySQL数据库的异步存储)
- pythontkinter详解(python3.6使用tkinter实现弹跳小球游戏)
- python爬虫怎么爬取vip资源(Python网络爬虫之爬取微博热搜)
- python入门练习网页(详解python项目实战:模拟登陆CSDN)
- python装饰器怎么设置(深入了解和应用Python 装饰器 @decorator)
- 成都轨道交通19号线二期全线电通(成都轨道交通19号线二期全线电通)
- 19号线二期全线电通 轨道交通项目最新进展来了(19号线二期全线电通)
- 涉及3条地铁线路 成都这4座轨道交通站点有新名字了(涉及3条地铁线路)
- 来了 成都轨道交通5条线路刷新 进度条(成都轨道交通5条线路刷新)
- 一部手机两套系统 OPPO Find X3的正确打开方式你知道吗(一部手机两套系统)
- OPPO用户看过来 汇总几个春节实用技巧,轻松搞定多设备联动玩法(汇总几个春节实用技巧)
热门推荐
- python udp通信(Python socket模块实现的udp通信功能示例)
- python 基于内容的推荐系统(不到40行代码用Python实现一个简单的推荐系统)
- python 常用模块函数(Python OOP类中的几种函数或方法总结)
- php7处理方案(PHP7 安装event扩展的实现方法)
- jvm运动数据区总结(JVM上高性能数据格式库包Apache Arrow入门和架构详解Gkatziouras)
- php包含哪些(php文件包含的几种方式总结)
- 阿里云服务器怎样设置ip(阿里云服务器ping不通解决办法云服务器搭建完环境访问不了ip解决办法)
- vue3 ref 的用法(Vue3中watchEffect的用途浅析)
- webapi 参数的传递
- idea社区版使用tomcat部署项目(基于IDEA部署Tomcat服务器的步骤详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9