python做中学(九)定时器函数的用法
程序中,经常用到这种,就是需要固定时间执行的,或者需要每隔一段时间执行的。这里经常用的就是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的基本用法是比较简单的,这个是不是对你有用呢?
参考文档:
1 http://c.biancheng.net/view/2629.html
python做中学(九)定时器函数的用法的更多相关文章
- python做中学(一)全局变量的用法
一段时间没有使用python来写代码,就发现以前学习的很多语法都忘了.看来还是当初这方面的项目做的好不够多,没有系统性的运用和学习,导致了很多语法不能顺手拈来.在接下来的这个项目中, 一定要把遇到的一 ...
- python做中学(八)匿名函数lambda的用法
匿名函数,顾名思义即没有名称的函数,和def定义的函数的最大区别在于匿名函数创建后返回函数本身(即匿名函数不需要return来返回值),表达式本身结果就是返回值,而def创建后则赋值给一个变量名,在P ...
- python做中学(四)main函数的用法
什么场景下会有main函数? 当该python脚本被作为模块(module)引入(import)时,其中的main()函数将不会被执行. main函数的作用? __name__ == '__main_ ...
- python做中学(二)bool()函数的用法
定义: bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. bool 是 int 的子类. 语法: 以下是 bool() 方法的语法: class bool([x] 参数 ...
- python做中学(七)ord() 函数
描述 ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII ...
- python做中学(六)os.getcwd() 的用法
概述 os.getcwd() 方法用于返回当前工作目录. 语法 getcwd()方法语法格式如下: os.getcwd() 参数 无 返回值 返回当前进程的工作目录. 实例 以下实例演示了 getcw ...
- python做中学(三)条件编译的用法
C代码中经常使用条件编译,python中该怎么用呢?Python没有像C或C或Java甚至Java一样编译,python文件被“即时”编译,您可以将其视为类似于Basic或Perl的解释语言 只需使用 ...
- python做中学(五)多线程的用法
多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条 ...
- python学习笔记之open函数的用法
先上一段代码 >>> f = open('1.txt','r'); >>> f.readline() #读取数据>>> f.close() #关闭 ...
随机推荐
- diango中让装了装饰器的函数的名字不是inner,而是原来的名字
让装了装饰器的函数的名字不是inner,而是原来的名字 from functools import wraps def wrapper(func): @wraps(func) # 复制了原来函数的名字 ...
- 12C-使用跨平台增量备份减少可移动表空间的停机时间 (Doc ID 2005729.1)
12C - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 20057 ...
- java 后端与前端Date类型与String类型互相转换(使用注解)
后端返回的类型中,直接定义Date类型,加上此注解,直接将Date类型转成自定义的格式给前端 class TestDateOutput{ @JsonFormat(pattern = "yyy ...
- Eureka集群
Eureka集群搭建 高可用集群配置 当注册中心扛不住高并发的时候,这时候 要用集群来扛: 普通操作 我们再新建两个module microservice-eureka-server-2002 m ...
- FTP安装及配置
在centos7安装ftp服务 yum install -y vsftpd 启动服务 systemctl start vsftpd 自启动 systemctl enable vsftpd 查看端口 注 ...
- php 判断空
结果: 结论:$a != false 这种判断方式比empty() 速度更快
- 洛谷 P4396 (离散化+莫队+树状数组)
### 洛谷P4396 题目链接 ### 题目大意: 有 n 个整数组成的数组,m 次询问,每次询问中有四个参数 l ,r,a,b .问你在[l,r] 的区间内的所有数中,值属于[a,b] 的数的个 ...
- mysql中concat函数的使用相关总结
concat(str1,str2) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. mysql> select concat('11','22','33') ...
- Scrapy框架-爬虫程序相关属性和方法汇总
一.爬虫项目类相关属性 name:爬虫任务的名称 allowed_domains:允许访问的网站 start_urls: 如果没有指定url,就从该列表中读取url来生成第一个请求 custom_se ...
- golang-方法和接口
1.方法 方法类似函数 ,多了一个接收者 ,接收者是指针指向结构体(也可以是值) ,方法与结构体绑定 (可以理解为模板定义方法) ,方法位于结构体内部 方法集可以理解就是多个方法 可以组合其他结构体方 ...