用Python編寫簡單的定時(shí)器的方法
下面介紹以threading模塊來實(shí)現(xiàn)定時(shí)器的方法。
首先介紹一個(gè)最簡單實(shí)現(xiàn):
import threading def say_sth(str): print str t = threading.Timer(2.0, say_sth,[str]) t.start() if __name__ == '__main__': timer = threading.Timer(2.0,say_sth,['i am here too.']) timer.start()
不清楚在某些特殊應(yīng)用場(chǎng)景下有什么缺陷否。
下面是所要介紹的定時(shí)器類的實(shí)現(xiàn):
class Timer(threading.Thread):
"""
very simple but useless timer.
"""
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
print "Buzzzz!! Time's up!"
class CountDownTimer(Timer):
"""
a timer that can counts down the seconds.
"""
def run(self):
counter = self.runTime
for sec in range(self.runTime):
print counter
time.sleep(1.0)
counter -= 1
print "Done"
class CountDownExec(CountDownTimer):
"""
a timer that execute an action at the end of the timer run.
"""
def __init__(self, seconds, action, args=[]):
self.args = args
self.action = action
CountDownTimer.__init__(self, seconds)
def run(self):
CountDownTimer.run(self)
self.action(self.args)
def myAction(args=[]):
print "Performing my action with args:"
print args
if __name__ == "__main__":
t = CountDownExec(3, myAction, ["hello", "world"])
t.start()
- python 定時(shí)器,輪詢定時(shí)器的實(shí)例
- 對(duì)python周期性定時(shí)器的示例詳解
- Python實(shí)現(xiàn)定時(shí)精度可調(diào)節(jié)的定時(shí)器
- Python定時(shí)器實(shí)例代碼
- python定時(shí)器(Timer)用法簡單實(shí)例
- wxPython定時(shí)器wx.Timer簡單應(yīng)用實(shí)例
- python使用線程封裝的一個(gè)簡單定時(shí)器類實(shí)例
- python通過線程實(shí)現(xiàn)定時(shí)器timer的方法
- python單線程實(shí)現(xiàn)多個(gè)定時(shí)器示例
- python定時(shí)器使用示例分享
- python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法
相關(guān)文章
如何在Win10系統(tǒng)使用Python3連接Hive
這篇文章主要介紹了如何在Win10系統(tǒng)使用Python3連接Hive,幫助大家更好的利用python讀取數(shù)據(jù),進(jìn)行探索、分析和挖掘工作。感興趣的朋友可以了解下2020-10-10
Python參數(shù)傳遞機(jī)制傳值和傳引用原理詳解
這篇文章主要介紹了Python參數(shù)傳遞機(jī)制傳值和傳引用原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python基于opencv的圖像壓縮算法實(shí)例分析
這篇文章主要介紹了Python基于opencv的圖像壓縮算法,結(jié)合實(shí)例形式分析了使用opencv進(jìn)行圖像壓縮的常用操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
探究Python多進(jìn)程編程下線程之間變量的共享問題
這篇文章主要介紹了探究Python多進(jìn)程編程下線程之間變量的共享問題,多進(jìn)程編程是Python學(xué)習(xí)進(jìn)階中的重要知識(shí),需要的朋友可以參考下2015-05-05
使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作
這篇文章主要介紹了使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
詳細(xì)解讀Python的web.py框架下的application.py模塊
這篇文章主要介紹了Python的web.py框架下的application.py模塊,作者深入分析了web.py的源碼,需要的朋友可以參考下2015-05-05
python?實(shí)現(xiàn)銀行卡號(hào)查詢銀行名稱和簡稱功能
這篇文章主要介紹了python?實(shí)現(xiàn)銀行卡號(hào)查詢銀行名稱和簡稱功能,本文通過實(shí)例代碼補(bǔ)充介紹了基于PyQT5+OpenCv實(shí)現(xiàn)銀行卡號(hào)識(shí)別功能,感興趣的朋友一起看看吧2023-11-11
python?tkinter實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python?tkinter實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

