程序中,经常用到这种,就是需要固定时间执行的,或者需要每隔一段时间执行的。这里经常用的就是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. MySQL Error Log 文件丢失导致The server quit without updating PID file启动失败的场景

    今天在做mysql sniff测试的时候,中间重启MySQL实例的过程中,出现了"The server quit without updating PID file"这个经典的错误 ...

  2. 数据库迁移导致Unknown character set: 'GBK' 应用异常

    https://blog.csdn.net/u013415591/article/details/82692242https://blog.csdn.net/gx_1_11_real/article/ ...

  3. admin配置与Mysql数据库连接

    admin配置管理数据库的框架:web版的数据库管理页面初始化数据库: python manage.py makemigrations python manage.py migrate启动项目:(创建 ...

  4. C++继承产生的问题

    今天写代码,用到了继承,忘了将父类中的私有成员改为protected,结果一调用父类地函数后,子类中的root指针直接变为了父类中的空的root.私有成员在继承后依然会保留,占一定的内存空间,但却没有 ...

  5. Web安全测试学习笔记-DVWA-SQL注入-2

    接上一篇SQL注入的学习笔记,上一篇我通过报错信息得知后台数据库是MySQL(这个信息非常重要~),然后通过SQL注入拿到了用户表的所有行,其实我们还可以通过MySQL的特性来拿更多的信息. 1. 获 ...

  6. Spring Boot可执行Jar包运行原理

    目录 1. 打可执行Jar包 2. 可执行Jar包内部结构 3. JarLauncher 4. 简单总结 5. 远程调试 Spring Boot有一个很方便的功能就是可以将应用打成可执行的Jar.那么 ...

  7. Java题库——Chapter9 String的用法

    1)Which code fragment would correctly identify the number of arguments passed via the command line t ...

  8. PAT 1009 Product of Polynomials 模拟

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

  9. maven工程指定web资源包,创建jsp文件

    进入项目的project structure. 选择web. 在右侧增加web资源包路径

  10. ubuntu18.10 上安装docker容器

    网上有的安装步骤太复杂,并且安装过程中容易出错,其它安装不难,只需一条命令即可. 安装成功后,使用命令查看docker状态 systemctl status docker 安装前更新下包源 sudo ...