python自己做个定时器_简单实现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自己做个定时器_简单实现python定时器相关推荐
- python编写接口自动化脚本_简单的python http接口自动化脚本
摘抄:今天给大家分享一个简单的python脚本,使用python进行http的接口测试,脚本很简单,逻辑是:读取excel写好的测试用例,然后根据excel中的用例内容进行调用,判断预期结果中的返回值 ...
- python能做财务分析吗_您可以使用Python进行财务规划和分析吗?
python能做财务分析吗 问题 (The Problem) If you work in the Financial Planning and Analysis area, chances are ...
- python函数分几种_简单了解Python中的几种函数
python是支持多种范型的语言,可以进行所谓函数式编程,其突出体现在有这么几个函数: filter.map.reduce.lambda.yield lambda lambda函数的使用方法:在lam ...
- python协同过滤调用包_简单的python协同过滤程序实例代码
本文研究的主要是python协同过滤程序的相关内容,具体介绍如下. 关于协同过滤的一个最经典的例子就是看电影,有时候不知道哪一部电影是我们喜欢的或者评分比较高的,那么通常的做法就是问问周围的朋友,看看 ...
- python字符串前面加上序号_简单了解python字符串前面加r,u的含义
这篇文章主要介绍了简单了解python字符串前面加r,u的含义,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 u/U:表示unicode字符串 不是 ...
- python人脸识别系统下载_简单的Python人脸识别系统
案例一 导入图片 思路: 1.导入库 2.加载图片 3.创建窗口 4.显示图片 5.暂停窗口 6.关闭窗口 # 1.导入库 import cv2 # 2.加载图片 img = cv2.imread(' ...
- [转载] python中的且语句_简单探讨python中的语句和语法
参考链接: 在Python中解包:并行分配之外 python程序结构 python"一切皆对象",这是接触python听到最多的总结了.在python中最基层的单位应该就是对象了, ...
- python 获取excel文本框_简单使用python做excel多文件批量搜索(带图形界面)(已更新)...
[Python] 纯文本查看 复制代码import xlwings as xw #引入xlwings处理excel from tkinter import * #引入tkinter处理界面 impor ...
- python类和对象课件_简单解释Python的类和对象
前言: 对象是模拟真实世界,把数据和程序进行封装 . 对象 = 属性+ 方法 我们需要用类来创造一个对象,就像我们要用图纸来造房子一样.在Python中函数名是以小写字母开头 ,类名是以大写字母开头. ...
最新文章
- Java 求1000以内的同构数_求1-10000之间的同构数
- php7 变量,变量在PHP7内部的实现(一)
- mybatis collection用法_Mybatis中强大的resultMap
- 身份验证和授权作为开源解决方案服务
- 【机器学习】机器学习从零到掌握之十二 -- 教你使用决策树预测隐形眼镜类型
- 3002基于二叉链表的树结构相等的判断(附WA的可能情况)
- pyechart图像示例与细节修改资料
- C语言在当代人工智能背景下的作用,人工智能对于当代社会的意义
- bp神经网络的训练方法,一文搞定bp神经网络
- ThinkBook 14P Win11网卡驱动掉了-解决方案
- centos本地yum源配置
- 如何控制局域网网速_图文版[许迎果讲科普] 常见的影响网速的因素
- 哈利波特信息站web网页课设报告(html+css)
- 大数据是什么?初学者怎样理解大数据技术
- mac软件全屏时候最顶上任务栏保留_一键整理 Mac 顶部菜单栏,这款免费工具 App 还你清爽...
- 从网上下载一个vue项目,跑起来的全过程
- 【PMP】核对单和核查表的区别
- YOLOv5+TensorRT+Win11(Python版)
- 计算机板块:外包引领软件业高成长
- Message的用法