程序中,经常用到这种,就是需要固定时间执行的,或者需要每隔一段时间执行的。这里经常用的就是Timer定时器。Thread 类有一个 Timer子类,该子类可用于控制指定函数在特定时间内执行一次。

可以用几个例子来说明Timer的用法,

一 最简单的用法,N s后(2s)后执行:

 #python3 example
from threading import Timer
import time def hello_test():
print("hello world") t = Timer(2.0,hello_test)
t.start()

运行结果:

➜  timer git:(master) ✗ py timer_test1.py
hello world

二  每隔一秒执行一次,执行十次:

 #python3 example
from threading import Timer
import time count = 0
def print_timer():
global t, count
print("count:%d new time: %s" % (count,time.ctime()))
count += 1 if count < 10:
t = Timer(1, print_timer)
t.start() t = Timer(1.0, print_timer)
t.start()

运行结果:

 ➜  timer git:(master) ✗ py timer_test2.py
count:0 new time: Tue Aug 20 14:20:13 2019
count:1 new time: Tue Aug 20 14:20:14 2019
count:2 new time: Tue Aug 20 14:20:15 2019
count:3 new time: Tue Aug 20 14:20:16 2019
count:4 new time: Tue Aug 20 14:20:17 2019
count:5 new time: Tue Aug 20 14:20:18 2019
count:6 new time: Tue Aug 20 14:20:19 2019
count:7 new time: Tue Aug 20 14:20:20 2019
count:8 new time: Tue Aug 20 14:20:21 2019
count:9 new time: Tue Aug 20 14:20:22 2019

三 带参数输入的timer,每隔一秒执行一次,执行十次:

 #python3 example
from threading import Timer
import time def print_val(cnt):
print("cnt:%d new time: %s" % (cnt,time.ctime()))
cnt += 1 if cnt < 10:
t = Timer(1, print_val,(cnt,))
t.start()
else:
return t = Timer(2.0, print_val,(1,))
t.start()

运行结果:

 ➜  timer git:(master) ✗ py timer_test.py
cnt:1 new time: Tue Aug 20 14:23:31 2019
cnt:2 new time: Tue Aug 20 14:23:32 2019
cnt:3 new time: Tue Aug 20 14:23:33 2019
cnt:4 new time: Tue Aug 20 14:23:34 2019
cnt:5 new time: Tue Aug 20 14:23:35 2019
cnt:6 new time: Tue Aug 20 14:23:36 2019
cnt:7 new time: Tue Aug 20 14:23:37 2019
cnt:8 new time: Tue Aug 20 14:23:38 2019
cnt:9 new time: Tue Aug 20 14:23:39 2019

从上面的例子可以看出,timer的基本用法是比较简单的,这个是不是对你有用呢?

参考文档:

http://c.biancheng.net/view/2629.html

  

python做中学(九)定时器函数的用法的更多相关文章

  1. python做中学(一)全局变量的用法

    一段时间没有使用python来写代码,就发现以前学习的很多语法都忘了.看来还是当初这方面的项目做的好不够多,没有系统性的运用和学习,导致了很多语法不能顺手拈来.在接下来的这个项目中, 一定要把遇到的一 ...

  2. python做中学(八)匿名函数lambda的用法

    匿名函数,顾名思义即没有名称的函数,和def定义的函数的最大区别在于匿名函数创建后返回函数本身(即匿名函数不需要return来返回值),表达式本身结果就是返回值,而def创建后则赋值给一个变量名,在P ...

  3. python做中学(四)main函数的用法

    什么场景下会有main函数? 当该python脚本被作为模块(module)引入(import)时,其中的main()函数将不会被执行. main函数的作用? __name__ == '__main_ ...

  4. python做中学(二)bool()函数的用法

    定义: bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. bool 是 int 的子类. 语法: 以下是 bool() 方法的语法: class bool([x] 参数 ...

  5. python做中学(七)ord() 函数

    描述 ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII ...

  6. python做中学(六)os.getcwd() 的用法

    概述 os.getcwd() 方法用于返回当前工作目录. 语法 getcwd()方法语法格式如下: os.getcwd() 参数 无 返回值 返回当前进程的工作目录. 实例 以下实例演示了 getcw ...

  7. python做中学(三)条件编译的用法

    C代码中经常使用条件编译,python中该怎么用呢?Python没有像C或C或Java甚至Java一样编译,python文件被“即时”编译,您可以将其视为类似于Basic或Perl的解释语言 只需使用 ...

  8. python做中学(五)多线程的用法

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条 ...

  9. python学习笔记之open函数的用法

    先上一段代码 >>> f = open('1.txt','r'); >>> f.readline() #读取数据>>> f.close() #关闭 ...

随机推荐

  1. 小程序模板template使用介绍

    template(模板):是可以在wxml中引用的代码,就是在wxml中引用公用的wxml类型的代码,它的作用类似于组件,因此这里简单的说明下template与Component (组件)的区别. t ...

  2. 在 Cocos2d-x 中添加自己的微博链接

    配置:OS X 10.10 + Xcode 6.0 + Cocos2d-x-3.2 一.Android 端代码 1.在 Cocos2dxActivity.java 中添加openUrl函数并导入响应包 ...

  3. SpringBoot启动过程源码分析

    学习博客:SpringBoot时序图分析启动过程

  4. C# 序列化和反序列化(xml 文件)

    序列化是将对象保存为文本文件或二进制文件: 反序列化则是读取文件信息,还原为对象: 序列化保存为文本内容,主要是 xml 和 json 两种,这里介绍序列化为 xml 文件的方式. 想要序列化,先要在 ...

  5. 解决安装tensorflow-gpu失败:Command "python setup.py egg_info"failed with error code 10 in.....

    按照https://blog.csdn.net/shawroad88/article/details/82222811前几步安装. 又有新的报错如下: 再运行运行代码安装setuptools pip ...

  6. openpyxl基本操作

    参考资料:OpenPyXL的使用教程(一) openpyxl 基本操作 # 创建xml from openpyxl import Workbook # 创建工作簿 wb = Workbook() # ...

  7. linux常用命令修改权限查看文档

    一.>和>>指令 >用于将执行结果写入后面的文件中: 把前方语句的结果存进文件,若文件不存在会自动创建 >:输出重定向 会覆盖原来文件内容 >>:追加重定向 ...

  8. 集合系列 List(五):Stack

    Stack 是先进后出的栈结构,其并不直接实现具体的逻辑,而是通过继承 Vector 类,调用 Vector 类的方法实现. public class Stack<E> extends V ...

  9. 使用 html2canvas 点击保存时把当前页面生成图片

     style:   #box{     background-image:url('./img/pone.png')     }   body:   <div id="box" ...

  10. QT使用QPainter加水印

    QT使用QPainter加水印 加水印的代码 //为QPixmap添加水印 void MainWindow::addMask(QPixmap& pm, const QString& t ...