国产探花免费观看_亚洲丰满少妇自慰呻吟_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() 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临汾市| 闻喜县| 禹城市| 内丘县| 郯城县| 安福县| 洪洞县| 靖江市| 宽甸| 中卫市| 定州市| 都匀市| 综艺| 澄迈县| 油尖旺区| 奎屯市| 博湖县| 蓝山县| 徐汇区| 上虞市| 含山县| 南木林县| 北京市| 上林县| 达孜县| 疏勒县| 安庆市| 潍坊市| 屯门区| 卫辉市| 肥东县| 登封市| 呼伦贝尔市| 卢氏县| 新巴尔虎左旗| 麻阳| 阳泉市| 翁源县| 龙江县| 诸城市| 民丰县|