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

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

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

下面介紹以threading模塊來(lái)實(shí)現(xiàn)定時(shí)器的方法。

首先介紹一個(gè)最簡(jiǎn)單實(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)用場(chǎ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)文章

最新評(píng)論

浮山县| 隆德县| 郁南县| 乌兰县| 丹东市| 图片| 湟中县| 蓬安县| 崇仁县| 长乐市| 浑源县| 青龙| 岗巴县| 双流县| 古田县| 巴马| 雷山县| 云和县| 龙门县| 洮南市| 安岳县| 湘潭市| 蓝田县| 鄄城县| 开江县| 开远市| 辽中县| 长沙市| 江门市| 宁津县| 德化县| 龙山县| 襄樊市| 岳西县| 通州市| 佳木斯市| 濮阳县| 高州市| 平舆县| 清水河县| 桂阳县|