Skip to content

Instantly share code, notes, and snippets.

@marconi
Created September 23, 2011 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marconi/ca286ba6e48148d8ac02 to your computer and use it in GitHub Desktop.
Save marconi/ca286ba6e48148d8ac02 to your computer and use it in GitHub Desktop.
Countdown using twisted LoopingCall class
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
class Countdown(object):
def __init__(self, counter, freq):
self.counter = counter
self.freq = freq
self.lc = LoopingCall(self.count)
self.lc.start(self.freq)
def count(self):
if self.counter == 0:
self.lc.stop()
reactor.stop()
else:
print "%s: %s" % (self.freq, self.counter)
self.counter -= 1
print 'Start!'
Countdown(5, 1)
Countdown(6, 2)
Countdown(7, 3)
reactor.run()
print 'Stop!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment