-
-
Save alpha-beta-soup/f3cb58b9da0a9feb2f00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Countdown(object): | |
n_counters = 0 # class-level variable: shared between all instances | |
def __init__(self, counter=5, delay=1): | |
self.counter = counter | |
self.delay = delay | |
Countdown.n_counters += 1 | |
def count(self): | |
if self.counter == 0: | |
Countdown.n_counters -= 1 | |
print "A countdown has finished, %s remaining" % Countdown.n_counters | |
if not Countdown.n_counters: | |
reactor.stop() | |
else: | |
print self.counter, '...' | |
self.counter -= 1 | |
reactor.callLater(self.delay, self.count) | |
from twisted.internet import reactor | |
reactor.callWhenRunning(Countdown(5).count) | |
reactor.callWhenRunning(Countdown(2, 5).count) | |
reactor.callWhenRunning(Countdown(10, 0.5).count) | |
print 'Start!' | |
reactor.run() | |
print 'Stop!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment