I am trying to teach myself some rudimentary Twisted programming thanks to this tutorial and many others. I have come to this current example which I Can't figure out why it's doing what it is doing.
Short summary: I have instantiated three reactors that count down from 5 to 1 with different delays in their counting. Only thing is, it looks like when the first counter (with the shortest delay) gets to 0, it stops not only its own reactor, but all the others.
#!/usr/bin/env python
class Countdown(object):
counter = 5
def count1(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...1'
self.counter -= 1
reactor.callLater(1, self.count1)
def count2(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...2'
self.counter -= 1
reactor.callLater(0.5, self.count2)
def count3(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...3'
self.counter -= 1
reactor.callLater(0.25, self.count3)
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count1)
reactor.callWhenRunning(Countdown().count2)
reactor.callWhenRunning(Countdown().count3)
print 'Start!'
reactor.run()
print 'Stop!'
Output
Start!
5 ...1
5 ...2
5 ...3
4 ...3
4 ...2
3 ...3
2 ...3
4 ...1
3 ...2
1 ...3
Stop!
I was under the impression that while all three counters should count down at their own speed and complete their 5->0 progression, the program would wait for them all to complete before exiting. Am I misunderstanding something in the ways of Twisted here?