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

首頁 > 編程 > Python > 正文

用Python編寫簡單的定時器的方法

2020-02-23 01:02:38
字體:
來源:轉載
供稿:網友

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

首先介紹一個最簡單實現:

import threadingdef 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()

不清楚在某些特殊應用場景下有什么缺陷否。

下面是所要介紹的定時器類的實現:

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() 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘德县| 平罗县| 兴海县| 合阳县| 龙泉市| 二手房| 武胜县| 龙井市| 海口市| 同心县| 枝江市| 武汉市| 炎陵县| 莱芜市| 康乐县| 乐业县| 正阳县| 武乡县| 措美县| 永康市| 林周县| 黄山市| 兴文县| 高邑县| 施甸县| 虹口区| 东明县| 蒙城县| 大荔县| 江油市| 保山市| 法库县| 丰镇市| 瓦房店市| 锡林浩特市| 雷州市| 任丘市| 台东市| 蕉岭县| 德阳市| 耒阳市|