Advertisement
Guest User

LoopingCallCountdown.py

a guest
Feb 14th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from twisted.internet.task import LoopingCall
  2. from twisted.internet import reactor
  3.  
  4. ''' LJ: Runs three different countdown objects using LoopingCall, each obj has different countdown interval.
  5.    Used Python 2.6.1 and Twisted 11.1.0
  6. '''
  7.  
  8. class Countdown(object):
  9.     #Initialize counter for particular countdown object
  10.     def __init__(self, countdowns, index):
  11.         self.counter = countdowns[index]
  12.  
  13.     #Performs the countdown until zero, decrements from the Countdowns toFinish counter.
  14.     #When countdowns toFinish reach zero, then we stop the reactor.
  15.     def count(self):
  16.         if self.counter:
  17.             print self.counter, "..."
  18.             self.counter -= 1
  19.             if self.counter == 0:
  20.                 self.loopCall.stop()
  21.                 global toFinish
  22.                 toFinish -= 1
  23.                 if toFinish == 0:
  24.                     reactor.stop()
  25.                    
  26.  
  27. print 'Start!'
  28.  
  29. #Below is a list of different countdowns we want to perform
  30. countdowns =[5,11,3]
  31. toFinish = len(countdowns)
  32.  
  33. #initialise basic countdown objects. Make sure this matches number found the countdowns array.
  34. countdown1 = Countdown(countdowns, 0)
  35. countdown2 = Countdown(countdowns, 1)
  36. countdown3 = Countdown(countdowns, 2)
  37.  
  38. #assign the loopingCall Object - must have separate line to start since start() returns a deferred.
  39. countdown1.loopCall = LoopingCall(countdown1.count)
  40. countdown1.loopCall.start(0.8)
  41. countdown2.loopCall = LoopingCall(countdown2.count)
  42. countdown2.loopCall.start(1.0)
  43. countdown3.loopCall = LoopingCall(countdown3.count)
  44. countdown3.loopCall.start(0.1)
  45.  
  46. reactor.run()
  47. print 'Stop!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement