方法一,使用线程中现成的:

这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法:

#! /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定时器常用的两种实现方法相关推荐

  1. python下timer定时器常用的两种实现方法

    方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: import threading import time def fun_ti ...

  2. python timer使用-python下timer定时器常用的两种实现方法

    方法一,使用线程中现成的: 这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: #! /usr/bin/python3 #! -*- conding: utf ...

  3. Json返回时间中出现乱码问题的两种解决方法

    Json返回时间中出现乱码问题的两种解决方法 参考文章: (1)Json返回时间中出现乱码问题的两种解决方法 (2)https://www.cnblogs.com/hanyinglong/archiv ...

  4. 在.NET中执行Async/Await的两种错误方法

    微信公众号:架构师高级俱乐部 关注可了解更多的编程,架构知识.问题或建议,请公众号留言; 如果你觉得此文对你有帮助,欢迎转发 在.NET中执行异步/等待的两种错误方法 在应用开发中,我们为了提高应用程 ...

  5. c++STL中的find()函数 有两种使用方法

    c++STL中的find()函数 有两种使用方法 方法一: 开头引头文件:中的函数 其调用形式为 find(start,end,value) start搜寻的起点,end搜寻的终点,要寻找的value ...

  6. Java中关于Arrays.sort的两种重载方法的理解

    前言 在java中重载排序方法的方法目前有两种,一种是实现Comparable接口的compareTo方法,还有一种是用比较器(comparator) 作为参数,其中比较器是实现了Comparator ...

  7. 计算机一级无法打开office,电脑中无法运行Office2016的两种解决方法

    最近,有些用户在电脑中安装完office2016后打开该程序时总是提示:无法启动此程序,因为计算机中丢失 api-ms-win-crt-stdio-l1-1-0.dll .那么,遇到这问题该怎么来解决 ...

  8. 风控模型中特征重要度的两种筛选方法

    在采用决策树算法建立模型的场景中,例如GBDT.XGBoost.LightGBM.Random Forest等,我们习惯通过Feature Importance指标作为特征筛选的重要方法之一.从特征定 ...

  9. python 合并word文件,在Python上的WordCloud中,我想合并两种语言

    In WordCloud on Python I would like to merge two languages ​​into one picture (English, Arabic) but ...

最新文章

  1. 教遗传算法人工智能玩超级马里奥大陆
  2. 平板python_Wacom平板电脑的Python示例
  3. c/c++获取文件大小的方法
  4. Python中flatten( ),matrix.A用法
  5. Java Spring MVC
  6. ARC078F - Mole and Abandoned Mine(状压DP)
  7. 计算机基础,你知道蓝屏的原因吗
  8. plex linux 数据目录,shareplex日常维护文档
  9. C# 委托与事件总结
  10. HDU 2896病毒侵袭
  11. 小猿圈分享Javascript技巧(下)
  12. mysql 触发器 sql日志_mysql日志触发器实现代码
  13. Unity 跳转第三方App
  14. 第二十九章、containers容器类部件QFrame框架部件详解
  15. ionic开发记账软件《易跟金》
  16. 麦克风有突突突的杂音
  17. 计算机的开机键的英语,每次开机出现英文(按任意键),才能进入电脑,好烦...
  18. 剑指offer 学习笔记 和为s的数字
  19. 阿里云视频点播(上传视频)服务最新版本使用方法(解决部分依赖无法下载或不存在问题)
  20. 前端的组件化和模块化

热门文章

  1. springboot中radis配置和使用【进阶二】
  2. 敏捷开发每日报告--day5
  3. Unity3D深入浅出 -组件与节点之间的调用关系
  4. 交换机的MAC地址作用
  5. Apache rewrite
  6. ADO.NET入门教程(三) 连接字符串,你小觑了吗?
  7. 《转》从程序员到项目经理(五):不是人人都懂的学习要点
  8. VS2008 + Silverlight初步学习
  9. Reflection带来的潜在威胁
  10. php如何设定隐藏四位号码,PHP问题:php手机号码中间四位如何隐藏?