python 定時器,輪詢定時器的實例
更新時間:2019年02月20日 09:38:12 作者:u013378306
今天小編就為大家分享一篇python 定時器,輪詢定時器的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
python 定時器默認(rèn)定時器只執(zhí)行一次,第一個參數(shù)單位S,幾秒后執(zhí)行
import threading
def fun_timer():
print('Hello Timer!')
timer = threading.Timer(1, fun_timer)
timer.start()
改成以下可以執(zhí)行多次
建立loop_timer.py
from threading import _Timer
class LoopTimer(_Timer):
"""Call a function after a specified number of seconds:
t = LoopTi
mer(30.0, f, args=[], kwargs={})
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
def __init__(self, interval, function, args=[], kwargs={}):
_Timer.__init__(self, interval, function, args, kwargs)
def run(self):
'''self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.set()'''
while True:
self.finished.wait(self.interval)
if self.finished.is_set():
self.finished.set()
break
self.function(*self.args, **self.kwargs)
調(diào)用
t = LoopTimer(120, fun_timer) t.start()
以上這篇python 定時器,輪詢定時器的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python常用標(biāo)準(zhǔn)庫之os模塊功能
這篇文章主要介紹了Python常用標(biāo)準(zhǔn)庫之os模塊功能,os模塊的主要功能有系統(tǒng)相關(guān)、目錄及文件操作、執(zhí)行命令和管理進程,其中的進程管理功能主要是Linux相關(guān)的,此處不做討論,對Python標(biāo)準(zhǔn)庫os相關(guān)知識感興趣的朋友跟隨小編一起看看吧2022-11-11
Python使用SQLAlchemy模塊實現(xiàn)操作數(shù)據(jù)庫
SQLAlchemy 是用Python編程語言開發(fā)的一個開源項目,它提供了SQL工具包和ORM對象關(guān)系映射工具,使用SQLAlchemy可以實現(xiàn)高效和高性能的數(shù)據(jù)庫訪問,下面我們就來學(xué)習(xí)一下SQLAlchemy模塊的具體應(yīng)用吧2023-11-11

