激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python通過線程實現定時器timer的方法

python通過線程實現定時器timer的方法

2019-12-03 14:31chongq Python

這篇文章主要介紹了python通過線程實現定時器timer的方法,涉及Python線程與定時器timer的使用技巧,需要的朋友可以參考下

本文實例講述了python通過線程實現定時器timer的方法。分享給大家供大家參考。具體分析如下:

這個python類實現了一個定時器效果,調用非常簡單,可以讓系統定時執行指定的函數

下面介紹以threading模塊來實現定時器的方法。

使用前先做一個簡單試驗:

  1. import threading 
  2. def sayhello(): 
  3.     print "hello world" 
  4.     global t    #Notice: use global variable! 
  5.     t = threading.Timer(5.0, sayhello) 
  6.     t.start() 
  7. t = threading.Timer(5.0, sayhello) 
  8. t.start() 

運行結果如下:

  1. >python hello.py 
  2. hello world 
  3. hello world 
  4. hello world 

下面是定時器類的實現:

  1. class Timer(threading.Thread): 
  2.     ""
  3.     very simple but useless timer. 
  4.     ""
  5.     def __init__(self, seconds): 
  6.         self.runTime = seconds 
  7.         threading.Thread.__init__(self) 
  8.     def run(self): 
  9.         time.sleep(self.runTime) 
  10.         print "Buzzzz!! Time's up!" 
  11. class CountDownTimer(Timer): 
  12.     ""
  13.     a timer that can counts down the seconds. 
  14.     ""
  15.     def run(self): 
  16.         counter = self.runTime 
  17.         for sec in range(self.runTime): 
  18.             print counter 
  19.             time.sleep(1.0) 
  20.             counter -= 1 
  21.         print "Done" 
  22. class CountDownExec(CountDownTimer): 
  23.     ""
  24.     a timer that execute an action at the end of the timer run. 
  25.     ""
  26.     def __init__(self, seconds, action, args=[]): 
  27.         self.args = args 
  28.         self.action = action 
  29.         CountDownTimer.__init__(self, seconds) 
  30.     def run(self): 
  31.         CountDownTimer.run(self) 
  32.         self.action(self.args) 
  33. def myAction(args=[]): 
  34.     print "Performing my action with args:" 
  35.     print args 
  36. if __name__ == "__main__"
  37.     t = CountDownExec(3, myAction, ["hello""world"]) 
  38.     t.start() 

以上代碼在Python 2.5.4中運行通過

希望本文所述對大家的Python程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄污在线观看 | 99国产精品自拍 | 成人一级免费 | 亚洲国产精品一区 | 日韩欧美高清片 | 在线观看精品视频 | 精品久久久久久久久久久aⅴ | 日韩一级片一区二区三区 | 国产日韩在线观看一区 | 国产一区二区免费在线观看 | 免费观看视频91 | 毛片福利| 日韩999 | 精品国产一级毛片 | av在线免费看网址 | 日美黄色片 | 色av成人天堂桃色av | 色精品国产 | 久久夜视频 | 精品亚洲一区二区三区 | 久久亚洲线观看视频 | 久久伊人国产精品 | 色妇视频 | 亚洲精品欧美二区三区中文字幕 | 91网址在线播放 | 嫩呦国产一区二区三区av | 免费毛片视频 | 亚洲情在线 | 免费一级片观看 | 91av亚洲| 91久久免费 | 久久久久久久久久久久网站 | 欧美一区二区黄色片 | 曰本三级日本三级日本三级 | 精品国产一区二区三区天美传媒 | 亚洲最新无码中文字幕久久 | 免费性爱视频 | 一级毛片高清 | 日本精品免费观看 | 久久久麻豆 | 美国人成人在线视频 |