Advertisement
Guest User

Twisted Countdown Etude 2

a guest
Dec 20th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # A solution to exercise 2 of the Twisted tutorial at http://krondo.com/?p=1333
  2.  
  3. import time
  4. from twisted.internet.task import LoopingCall
  5.  
  6. class CountdownMonitor(object):
  7.  
  8.     def __init__(self):
  9.         self.countdowns = []
  10.  
  11.     def register(self, *args):
  12.         self.countdowns.extend(args)
  13.  
  14.     def start(self):
  15.         self.startTime = time.time()
  16.  
  17.     def timeElapsed(self):
  18.         return time.time() - self.startTime
  19.  
  20.     def finalize(self, ctdn):
  21.         self.countdowns.remove(ctdn)
  22.         if not self.countdowns:
  23.             reactor.callLater(0, reactor.stop)
  24.  
  25. class Countdown(object):
  26.  
  27.     def __init__(self, name, ticks, rate, monitor):
  28.         self.name, self.counter, self.rate, self.monitor = \
  29.             name, ticks, rate, monitor
  30.         self.looper = LoopingCall(self.count)
  31.         monitor.register(self)
  32.  
  33.     def start(self):
  34.         self.looper.start(self.rate)
  35.  
  36.     def count(self):
  37.         if self.counter == 0:
  38.             self.looper.stop()
  39.             print 'Counter {} complete! ({:.3f} sec)'.format(self.name, monitor.timeElapsed())
  40.             self.monitor.finalize(self)
  41.         else:
  42.             print '{}: {:>2} ticks left at {} sec/tick ...'.format(self.name, self.counter, self.rate)
  43.             self.counter -= 1
  44.  
  45. from twisted.internet import reactor
  46.  
  47. monitor = CountdownMonitor()
  48. reactor.callWhenRunning(monitor.start)
  49.  
  50. data = [['one', 5, 1.0], ['two', 13, .3], ['tri', 8, .8]]
  51. for name, ticks, rate in data:
  52.     reactor.callWhenRunning(Countdown(name, ticks, rate, monitor).start)
  53.  
  54. print 'Start!'
  55. reactor.run()
  56. print 'Stop!'
  57.  
  58.  
  59. #### Output ####
  60.  
  61. Start!
  62. one:  5 ticks left at 1.0 sec/tick ...
  63. two: 13 ticks left at 0.3 sec/tick ...
  64. tri:  8 ticks left at 0.8 sec/tick ...
  65. two: 12 ticks left at 0.3 sec/tick ...
  66. two: 11 ticks left at 0.3 sec/tick ...
  67. tri:  7 ticks left at 0.8 sec/tick ...
  68. two: 10 ticks left at 0.3 sec/tick ...
  69. one:  4 ticks left at 1.0 sec/tick ...
  70. two:  9 ticks left at 0.3 sec/tick ...
  71. two:  8 ticks left at 0.3 sec/tick ...
  72. tri:  6 ticks left at 0.8 sec/tick ...
  73. two:  7 ticks left at 0.3 sec/tick ...
  74. one:  3 ticks left at 1.0 sec/tick ...
  75. two:  6 ticks left at 0.3 sec/tick ...
  76. two:  5 ticks left at 0.3 sec/tick ...
  77. tri:  5 ticks left at 0.8 sec/tick ...
  78. two:  4 ticks left at 0.3 sec/tick ...
  79. one:  2 ticks left at 1.0 sec/tick ...
  80. two:  3 ticks left at 0.3 sec/tick ...
  81. tri:  4 ticks left at 0.8 sec/tick ...
  82. two:  2 ticks left at 0.3 sec/tick ...
  83. two:  1 ticks left at 0.3 sec/tick ...
  84. Counter two complete! (3.900 sec)
  85. one:  1 ticks left at 1.0 sec/tick ...
  86. tri:  3 ticks left at 0.8 sec/tick ...
  87. tri:  2 ticks left at 0.8 sec/tick ...
  88. Counter one complete! (5.000 sec)
  89. tri:  1 ticks left at 0.8 sec/tick ...
  90. Counter tri complete! (6.400 sec)
  91. Stop!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement