Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.internet.task import LoopingCall
- from twisted.internet import reactor
- ''' LJ: Runs three different countdown objects using LoopingCall, each obj has different countdown interval.
- Used Python 2.6.1 and Twisted 11.1.0
- '''
- class Countdown(object):
- #Initialize counter for particular countdown object
- def __init__(self, countdowns, index):
- self.counter = countdowns[index]
- #Performs the countdown until zero, decrements from the Countdowns toFinish counter.
- #When countdowns toFinish reach zero, then we stop the reactor.
- def count(self):
- if self.counter:
- print self.counter, "..."
- self.counter -= 1
- if self.counter == 0:
- self.loopCall.stop()
- global toFinish
- toFinish -= 1
- if toFinish == 0:
- reactor.stop()
- print 'Start!'
- #Below is a list of different countdowns we want to perform
- countdowns =[5,11,3]
- toFinish = len(countdowns)
- #initialise basic countdown objects. Make sure this matches number found the countdowns array.
- countdown1 = Countdown(countdowns, 0)
- countdown2 = Countdown(countdowns, 1)
- countdown3 = Countdown(countdowns, 2)
- #assign the loopingCall Object - must have separate line to start since start() returns a deferred.
- countdown1.loopCall = LoopingCall(countdown1.count)
- countdown1.loopCall.start(0.8)
- countdown2.loopCall = LoopingCall(countdown2.count)
- countdown2.loopCall.start(1.0)
- countdown3.loopCall = LoopingCall(countdown3.count)
- countdown3.loopCall.start(0.1)
- reactor.run()
- print 'Stop!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement