python timer使用-python中timer定时器常用的两种实现方法
方法一,使用线程中现成的:
这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法:
#! /usr/bin/python3
#! -*- conding: utf-8 -*-
import threading
import time
def fun_timer():
print(time.strftime('%Y-%m-%d %H:%M:%S'))
global timer
timer = 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:
这种方法使用比较灵活,可根据自身的东西来添自身的需求:
import time
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class"""
class Timer:
def __init__(self):
self._start_time = None
def 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_time
self._start_time = None
print(f"Elapsed time: {elapsed_time:0.4f} seconds")
python timer使用-python中timer定时器常用的两种实现方法相关推荐
- python下timer定时器常用的两种实现方法
方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: import threading import time def fun_ti ...
- python timer使用-python下timer定时器常用的两种实现方法
方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: #! /usr/bin/python3 #! -*- conding: utf ...
- Json返回时间中出现乱码问题的两种解决方法
Json返回时间中出现乱码问题的两种解决方法 参考文章: (1)Json返回时间中出现乱码问题的两种解决方法 (2)https://www.cnblogs.com/hanyinglong/archiv ...
- 在.NET中执行Async/Await的两种错误方法
微信公众号:架构师高级俱乐部 关注可了解更多的编程,架构知识.问题或建议,请公众号留言; 如果你觉得此文对你有帮助,欢迎转发 在.NET中执行异步/等待的两种错误方法 在应用开发中,我们为了提高应用程 ...
- c++STL中的find()函数 有两种使用方法
c++STL中的find()函数 有两种使用方法 方法一: 开头引头文件:中的函数 其调用形式为 find(start,end,value) start搜寻的起点,end搜寻的终点,要寻找的value ...
- Java中关于Arrays.sort的两种重载方法的理解
前言 在java中重载排序方法的方法目前有两种,一种是实现Comparable接口的compareTo方法,还有一种是用比较器(comparator) 作为参数,其中比较器是实现了Comparator ...
- 计算机一级无法打开office,电脑中无法运行Office2016的两种解决方法
最近,有些用户在电脑中安装完office2016后打开该程序时总是提示:无法启动此程序,因为计算机中丢失 api-ms-win-crt-stdio-l1-1-0.dll .那么,遇到这问题该怎么来解决 ...
- 风控模型中特征重要度的两种筛选方法
在采用决策树算法建立模型的场景中,例如GBDT.XGBoost.LightGBM.Random Forest等,我们习惯通过Feature Importance指标作为特征筛选的重要方法之一.从特征定 ...
- python 合并word文件,在Python上的WordCloud中,我想合并两种语言
In WordCloud on Python I would like to merge two languages into one picture (English, Arabic) but ...
最新文章
- 教遗传算法人工智能玩超级马里奥大陆
- 平板python_Wacom平板电脑的Python示例
- c/c++获取文件大小的方法
- Python中flatten( ),matrix.A用法
- Java Spring MVC
- ARC078F - Mole and Abandoned Mine(状压DP)
- 计算机基础,你知道蓝屏的原因吗
- plex linux 数据目录,shareplex日常维护文档
- C# 委托与事件总结
- HDU 2896病毒侵袭
- 小猿圈分享Javascript技巧(下)
- mysql 触发器 sql日志_mysql日志触发器实现代码
- Unity 跳转第三方App
- 第二十九章、containers容器类部件QFrame框架部件详解
- ionic开发记账软件《易跟金》
- 麦克风有突突突的杂音
- 计算机的开机键的英语,每次开机出现英文(按任意键),才能进入电脑,好烦...
- 剑指offer 学习笔记 和为s的数字
- 阿里云视频点播(上传视频)服务最新版本使用方法(解决部分依赖无法下载或不存在问题)
- 前端的组件化和模块化