最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用Python編寫簡單的定時(shí)器的方法

 更新時(shí)間:2015年05月02日 15:11:43   作者:PandaraWen  
這篇文章主要介紹了用Python編寫簡單的定時(shí)器的方法,主要用到了Python中的threading模塊,需要的朋友可以參考下

下面介紹以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)用場景下有什么缺陷否。

下面是所要介紹的定時(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() 

相關(guān)文章

最新評論

南投县| 水城县| 大埔县| 辛集市| 花垣县| 溆浦县| 高州市| 鄂托克旗| 界首市| 金秀| 舟曲县| 报价| 弥渡县| 宜良县| 庄浪县| 晋州市| 泸西县| 中宁县| 嘉黎县| 泸水县| 永泰县| 林口县| 旬邑县| 延津县| 河北省| 忻城县| 宝丰县| 康平县| 纳雍县| 新民市| 天峨县| 泰兴市| 买车| 景德镇市| 满洲里市| 屯昌县| 盱眙县| 简阳市| 绍兴县| 曲松县| 九台市|