程序中,经常用到这种,就是需要固定时间执行的,或者需要每隔一段时间执行的。这里经常用的就是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. windows下安装mysql教程

    1.下载安装包-根据自己电脑系统选择合适的版本: https://dev.mysql.com/downloads/mysql/ 2.配置环境变量 2.1 解压所下载的压缩包 2.2 环境变量 win ...

  2. Vue-cli脚手架 安装 并创建项目--命令

    检查是否有 node - v 安装Vue-cli npm install -g vue-cli 安装好后,执行 vue list可以看到很多实用的模板,我这里实用的webpack 初始化模板 vue ...

  3. 使用Apollo动态修改线上数据源

    前言 最近需要实现一个功能,动态刷新线上数据源环境,下面来使用Apollo配置中心和Spring提供的AbstractRoutingDataSource来实现. 具体实现 Apollo是携程开源的统一 ...

  4. Dockerfile优化

    总结: 1.编写.dockerignore文件 2.容器只运行单个应用 3.将多个RUN指令合并为一个 4.基础镜像的标签不要用latest 5.每个RUN指令后删除多余文件 6.选择合适的基础镜像( ...

  5. centos和Ubuntu系统最小化安装基础命令

    CentOS系统常用的基础软件如下 yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \ pcre-devel openssl o ...

  6. Linux_更改时区和利用Crontab同步时间

    一.更改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.Crontab时间同步 crontab -e   #crontab编辑 */5 ...

  7. Centos7下oracle配置(详细)

    一.硬件配置 CentOS7@VMware® Workstation 15 Pro,分配资源:CPU:2颗,内存:4GB,硬盘空间:30GB 二.软件准备  linux.x64_11gR2_datab ...

  8. 关于ssh的几个功能

    这个有许多介绍的. 通过ssh可以实现远程shell命令行登录,x window登录,端口转发,scp文件复制,本地与远程命令间的管道连接,sftp文件传输与管理,包括同步,以及rsync文件同步,还 ...

  9. Python的map方法的应用

    Map方法,第一个参数要写一个匿名函数表达式,或者是一个函数引用,第二个第三个往后都是表达式用到的参数,参数一般是可迭代的 1.比如下面这个map方法,两个参数,第一个 lambda x: x*x是匿 ...

  10. libwebrtc & libmediasoupclient编译

    本文简单介绍在Ubuntu下libwebrtc的编译过程. 由于网速限制,实际编译过程是在远程vps上编译滴. 系统环境 Ubuntu 18.04系统的虚拟主机. root@vultr:~# pwd ...