搜尋
首頁後端開發Python教學python實作循環定時器的方法介紹(附程式碼)

這篇文章帶給大家的內容是關於python實現循環定時器的方法介紹(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

python 如何寫一個定時器,循環定時要做某一操作呢?

Timer 物件

from threading import Timer
def hello(): 
    print "hello, world" 
   
t = Timer(10.0, hello) 
t.start()

10秒後輸出:

hello, world

重點研究 t = Timer(10.0, hello) 這句程式碼,python 提供了一個Timer 對象,它會在指定的時間後執行某一操作;它的完整形式:

class threading.Timer(interval, function, args=[], kwargs={})

interval 是時間間隔,function 是可呼叫的對象,args 和 kwargs 會作為 function 的參數。

注意:這裡只會執行一次 function,而不會一直定時執行,且 Timer 在執行操作的時候會建立一個新的執行緒。

Timer 在 python2 和 python3 有點區別:

# python2.7
def Timer(*args, **kwargs):
    return _Timer(*args, **kwargs)
# python3.7
class Timer(Thread):
    pass

在 python3,Timer 是 Thread 的子類別;在 python2,_Timer 是 Thread 的子類,而Tim 只是工廠類型的方法Tim _Thread 的子類,而Tim 只是工廠的方法。

上面的程式碼只會列印一次 hello, world 後退出,那麼如何循環間隔列印呢?

粗糙的循環計時器

一種方法是在 function 裡繼續註冊一個Timer,這樣就可以在下一個 interval 繼續執行 function;

from threading import Timer
def hello(): 
    print "hello, world" 
    Timer(10.0, hello) .start()

t = Timer(10.0, hello) 
t.start()

每隔10 秒

每隔10 秒

每隔10 秒
每隔10 秒輸出一個 hello, world。

達到效果了,但這裡面好像有點問題。回到 Timer 本身,它是一個 thread,每次循環間隔操作,系統都要創建一個線程,然後再回收,這對系統來說開銷很大。如果時間間隔 interval 很短,系統會一下子創建很多線程,這些線程很難快速回收,導致系統記憶體和cpu資源被消耗掉。

所以不主張在 function 裡繼續註冊一個 Timer。

更pythonic 循環定時器

這裡有更pythonic 的方法:

from threading import _Timer
def hello():
     print "hello, world"
class RepeatingTimer(_Timer): 
    def run(self):
        while not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
            self.finished.wait(self.interval)
t = RepeatingTimer(10.0, hello)
t.start()
重點研究 RepeatingTimer 類,它繼承了 threading._Timer,但是重寫了父類的run 方法。這是 Python2 的寫法,python3 中 RepeatingTimer 應該要繼承 threading.Timer。

為什麼要重寫 Thread 的 run 方法?

_Timer 是個 Thread 子類,我們先來看看 Thread 類別的 run 用法。

from threading import Thread
def hello():
     print "hello, world"
# 继承 Thread
class MyThread(Thread):
    # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
    def run(self):
        hello()
t = MyThread()
t.start()
Thread 物件的完整定義:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
其中 run 方法程式碼:

class Thread(_Verbose):
    def run(self):
        try:
            if self.__target:
                self.__target(*self.__args, **self.__kwargs)
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self.__target, self.__args, self.__kwargs
標準的 run 方法用於執行使用者傳入建構函數的 target 方法。子類別可以重寫 run 方法,把要執行的程式碼寫到 run 裡面,執行緒在建立後,使用者呼叫 start() 方法會執行 run() 方法。

所以 RepeatingTimer 重寫 _Timer 的 run() 方法,可以改變執行緒的執行體,當我們呼叫 RepeatingTimer 的 start() 方法時會執行我們重寫的 run() 方法。

再看看 RepeatingTimer 類別中的 while not self.finished.is_set() 語句,self.finished.is_set() 直到 True 才會退出循環,計時器才會退出。 finished 是 threading.Event 物件。一個 Event 物件管理一個flag 標誌,它能被 set() 方法設定為True,也能被 clear() 方法設定為False,呼叫 wait([timeout]) 執行緒會一直sleep 到flag 為True 或逾時時間到達。

我們知道計時器有一個 cancel() 方法可以提前取消操作。它其實是呼叫 Event.clear() 方法提前讓 wait 方法結束等待,並且判斷在 flag 為 true 的情況下不執行定時器操作。特定的程式碼:###
class _Timer(Thread):
    """Call a function after a specified number of seconds:
            t = Timer(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={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()

    def cancel(self):
        """Stop the timer if it hasn't finished yet"""
        self.finished.set()

    def run(self):
        self.finished.wait(self.interval)
        if not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
        self.finished.set()
###所以 RepeatingTimer 的 run 方法會一直執行 while 循環體,在循環體了會執行使用者傳入的 function 對象,並等待指定的時間。當使用者想要退出計時器時,只要呼叫 cancel 方法,將 flag 置為 True 便不會繼續執行循環體了。這樣便完成了一個還不錯的循環定時器。 ###

以上是python實作循環定時器的方法介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:segmentfault。如有侵權,請聯絡admin@php.cn刪除
2小時的Python計劃:一種現實的方法2小時的Python計劃:一種現實的方法Apr 11, 2025 am 12:04 AM

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python:探索其主要應用程序Python:探索其主要應用程序Apr 10, 2025 am 09:41 AM

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

您可以在2小時內學到多少python?您可以在2小時內學到多少python?Apr 09, 2025 pm 04:33 PM

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?Apr 02, 2025 am 07:18 AM

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Apr 02, 2025 am 07:12 AM

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何提高jieba分詞在景區評論分析中的準確性?如何提高jieba分詞在景區評論分析中的準確性?Apr 02, 2025 am 07:09 AM

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?如何使用正則表達式匹配到第一個閉合標籤就停止?Apr 02, 2025 am 07:06 AM

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境