I am new to twisted and python, I am reading twisted python.
registering call
reactor.callLater(_interval, self.count, *args)
I have created Countdown class and it contains count function, I am calling it three time
reactor.callWhenRunning(Countdown().count, 1)
reactor.callWhenRunning(Countdown().count, 2)
reactor.callWhenRunning(Countdown().count, 3)
with different time interval, and I need to stop reactor when all call is finished. So is in twisted API have some method to know all call is completed.
My Code is
class Countdown(object):
counter = 5
def count(self, *args):
_interval = args[0]
name = args[1]
if self.counter == 0 and name == "Third Call":
reactor.stop()
elif self.counter == 0:
print name, " Finished..!"
else:
print self.counter, '...'
self.counter -= 1
reactor.callLater(_interval, self.count, *args)
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count, 1, "First Call")
reactor.callWhenRunning(Countdown().count, 2, "Second Call")
reactor.callWhenRunning(Countdown().count, 3, "Third Call")
print 'Start!'
reactor.run()
print 'Stop!'
Now I am using if self.counter == 0 and name == "Third Call": to prevent my all counter process is finished. So now I need to know, is there in twisted have any built in method to know all call is finished or my all counter call is finished.
Twisted has an API for working with a list of events: DeferredList http://twistedmatrix.com/documents/current/core/howto/defer.html#auto8
Here is a small example how this might work:
from twisted.internet import defer
from twisted.internet import reactor
def delayedFunction(dF):
print('I was called')
dF.success(True)
class Counter(object):
def timeOffsetExecute(self,delay):
dF = defer.Deferred()
reactor.callLater(delay,delayedFunction,dF)
return dF
def onAllResult(val):
print 'All delayed functions called'
reactor.stop()
cp = Counter()
dl = defer.DeferredList([cp.timeOffsetExecute(1), cp.timeOffsetExecute(3), cp.timeOffsetExecute(9)], consumeErrors=True)
dl.addCallback(onAllResult)
reactor.run()