python下timer定时器常用的两种实现方法
方法一,使用线程中现成的:
这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法:
import threading
import time
def fun_timer():print(time.strftime('%Y-%m-%d %H:%M:%S'))global timertimer = threading.Timer(2,fun_timer)timer.start();
timer = threading.Timer(1,fun_timer)
timer.start();
time.sleep(5)
timer.cancel()
print(time.strftime('%Y-%m-%d %H:%M:%S'))
方法二,根据time中的来定义timer:
这种方法使用比较灵活,可根据自身的东西来添自身的需求:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import timeclass TimerError(Exception):"""A custom exception used to report errors in use of Timer class"""class Timer:def __init__(self):self._start_time = Nonedef start(self):"""Start a new timer"""if self._start_time is not None:raise TimerError(f"Timer is running. Use .stop() to stop it")self._start_time = time.perf_counter()def stop(self):"""Stop the timer, and report the elapsed time"""if self._start_time is None:raise TimerError(f"Timer is not running. Use .start() to start it")elapsed_time = time.perf_counter() - self._start_timeself._start_time = Noneprint(f"Elapsed time: {elapsed_time:0.4f} seconds")
python下timer定时器常用的两种实现方法相关推荐
- python timer使用-python下timer定时器常用的两种实现方法
方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: #! /usr/bin/python3 #! -*- conding: utf ...
- python timer使用-python中timer定时器常用的两种实现方法
方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: #! /usr/bin/python3 #! -*- conding: utf ...
- [Win+Python]使用python接口测试时302重定向的两种处理方法
目录 前言 1 接口简介 2 代码实现 2.1 自动重定向 2.2 跟随重定向 前言 我将在本文中简单介绍使用python进行接口测试时,遇到302重定向时的一些测试心得. 本文涉及实例中的一些关键信 ...
- Java最常用的两种随机数生成方法
随机数生成方法 第一种(new Random()) r.nextInt():产生整数范围的随机数(均匀分布) 应用示例:生成10~20之间的随机数: (1)范围:20-10=10,即随机数范围跨度为1 ...
- Python timeit 中的计时器的两种使用方法
一.直接使用timeit(),repeat() 1.timeit # 代码结构: # from timeit import timeit 模块引入 # time1 = timeit(" ...
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
From: http://www.cnblogs.com/lanxuezaipiao/p/3703988.html 导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列 ...
- python ioc di_Spring介绍,IOC(控制反转),DI(依赖注入)介绍及两种注入方法
Spring介绍,IOC(控制反转),DI(依赖注入)介绍及两种注入方法 第一中方法:在xml文件中注入: (1)开源的轻量级的应用开发框架 特点:a.简化开发:b.解耦:c.集成: 原理对象与对象之 ...
- 【Android 异步操作】Timer 定时器 ( Timer 与 TimerTask 基本使用 | Timer 定时器常用用法 | Timer 源码分析 )
文章目录 一.Timer 定时器基本使用 二.Timer 定时器常用用法 三.Timer 源码分析 四.Timer 部分源码注释 五.源码及资源下载 参考文档 : Timer 定时器 API 文档 T ...
- 【NLP-NER】命名实体识别中最常用的两种深度学习模型
命名实体识别(Named Entity Recognition,NER)是NLP中一项非常基础的任务.NER是信息提取.问答系统.句法分析.机器翻译等众多NLP任务的重要基础工具. 上一期我们介绍了N ...
最新文章
- centos 6.5 启动时卡在进度条位置无法进入系统解决办法。
- Linux vim开启/关闭代码着色(高亮显示)
- SQLite 入门教程(四)增删改查,有讲究
- c#设计模式-建造者模式
- 数据库知识:SQLServer变量相关知识介绍
- Visual Studio的未来特性展望
- zoj 1115 Digital Roots
- 二改注册登录版素材代下载搜索引擎系统源码,自带火车头采集
- Tigase数据库结构(1)
- [thinkphp 5.0源码阅读] 缓存(一)
- sqlalchemy query
- 联想高校AI精英挑战赛总冠军出炉!助力中国迎来智能变革
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径1...
- 探讨专线与家用宽带的区别
- Excel 2016 对数据做线性回归分析步骤
- TwinCAT3 EL6070-1648授权模块使用
- SVN和git的优缺点比较
- 阿里巴巴实习生初面面经
- Qt设置鼠标光标样式
- 微信 心理测试 软件,敢不敢做一份微信版心理测试?