国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

Python timer定時器兩種常用方法解析

2024-09-09 19:03:37
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python timer定時器兩種常用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

方法一,使用線程中現成的:

這種一般比較常用,特別是在線程中的使用方法,下面是一個例子能夠很清楚的說明它的具體使用方法:

#! /usr/bin/python3#! -*- conding: utf-8 -*-import threadingimport timedef 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 timeclass 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")

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網之家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱阳市| 阜南县| 民丰县| 黎平县| 孟连| 大姚县| 镇安县| 大田县| 临夏市| 太康县| 乌恰县| 德州市| 安丘市| 浮梁县| 米易县| 红原县| 津南区| 铁力市| 盐源县| 肃北| 石狮市| 金平| 朝阳市| 龙州县| 临夏县| 开阳县| 大竹县| 永清县| 遵化市| 皮山县| 北票市| 白河县| 汉源县| 修武县| 社旗县| 鲜城| 三原县| 铁力市| 铁力市| 富顺县| 洛宁县|