Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A solution to the Twisted tutorial exercise suggested in the comment at
- # http://krondo.com/?p=1333&cpage=1#comment-20927
- import time
- from twisted.internet.task import LoopingCall
- class CountdownMonitor(object):
- def __init__(self, rate=.2):
- self.rate = .2
- self.countdowns = []
- self.finishedCountdowns = []
- self.looper = LoopingCall(self.poll)
- def register(self, ctdn):
- self.countdowns.append(ctdn)
- reactor.callWhenRunning(ctdn.start)
- def start(self):
- self.startTime = time.time()
- self.looper.start(self.rate)
- def poll(self):
- for ctdn in self.countdowns:
- endTime = ctdn.endTime
- if endTime:
- self.countdowns.remove(ctdn)
- self.finishedCountdowns.append((ctdn.name, endTime - self.startTime))
- if not self.countdowns:
- for c, t in self.finishedCountdowns:
- print 'Countdown {} finished in {:.3f} sec'.format(c,t)
- reactor.callLater(0, reactor.stop)
- class Countdown(object):
- def __init__(self, name, ticks, rate):
- self.name, self.counter, self.rate = name, ticks, rate
- self.looper = LoopingCall(self.count)
- self.endTime = None
- def start(self):
- self.looper.start(self.rate)
- def count(self):
- if self.counter == 0:
- self.endTime = time.time()
- self.looper.stop()
- print 'Counter {} complete!'.format(self.name)
- else:
- print '{}: {:>2} ticks left at {} sec/tick ...'.format(self.name, self.counter, self.rate)
- self.counter -= 1
- from twisted.internet import reactor
- monitor = CountdownMonitor()
- reactor.callWhenRunning(monitor.start)
- data = [['one', 5, 1.0], ['two', 13, .3], ['tri', 8, .8]]
- for name, ticks, rate in data:
- monitor.register(Countdown(name, ticks, rate))
- print 'Start!'
- reactor.run()
- print 'Stop!'
- #### Output ####
- Start!
- one: 5 ticks left at 1.0 sec/tick ...
- two: 13 ticks left at 0.3 sec/tick ...
- tri: 8 ticks left at 0.8 sec/tick ...
- two: 12 ticks left at 0.3 sec/tick ...
- two: 11 ticks left at 0.3 sec/tick ...
- tri: 7 ticks left at 0.8 sec/tick ...
- two: 10 ticks left at 0.3 sec/tick ...
- one: 4 ticks left at 1.0 sec/tick ...
- two: 9 ticks left at 0.3 sec/tick ...
- two: 8 ticks left at 0.3 sec/tick ...
- tri: 6 ticks left at 0.8 sec/tick ...
- two: 7 ticks left at 0.3 sec/tick ...
- one: 3 ticks left at 1.0 sec/tick ...
- two: 6 ticks left at 0.3 sec/tick ...
- two: 5 ticks left at 0.3 sec/tick ...
- tri: 5 ticks left at 0.8 sec/tick ...
- two: 4 ticks left at 0.3 sec/tick ...
- one: 2 ticks left at 1.0 sec/tick ...
- two: 3 ticks left at 0.3 sec/tick ...
- tri: 4 ticks left at 0.8 sec/tick ...
- two: 2 ticks left at 0.3 sec/tick ...
- two: 1 ticks left at 0.3 sec/tick ...
- Counter two complete!
- one: 1 ticks left at 1.0 sec/tick ...
- tri: 3 ticks left at 0.8 sec/tick ...
- tri: 2 ticks left at 0.8 sec/tick ...
- Counter one complete!
- tri: 1 ticks left at 0.8 sec/tick ...
- Counter tri complete!
- Countdown two finished in 3.900 sec
- Countdown one finished in 5.000 sec
- Countdown tri finished in 6.400 sec
- Stop!
Advertisement
Add Comment
Please, Sign In to add comment