[Twisted-Python] Waiting for multiple events
Itamar Shtull-Trauring
twisted at itamarst.org
Wed Feb 20 04:47:07 MST 2002
Andrew Bennetts wrote:
> I have a collection of Deferreds. When they all complete, I want to trigger
> a "all complete" callback (probably via a Deferred). Alternatively, upon an
> error from any of the callbacks, I want to trigger the errback of that
> "all complete" Deferred.
>
> Is there something already written to do this? Or do I need to write it
> myself?
# this code is untested
class WaitForCallbacks:
def __init__(self, count, finishedDeferred):
self.count = count
self.callbacks = 0
self.finishedDeferred = finishedDeferred
def callback(self, result):
self.callbacks = self.callbacks + 1
if self.callbacks == self.count:
self.finishedDeferred.callback(result)
return result
def errback(self, err):
self.finishedDeferred.errback(err)
# then you make an instance of this class:
o = WaitForCallbacks(5, allComplete)
# any deferred D we want to connect, we do:
D.addCallbacks(o.callback, o.errback)
More information about the Twisted-Python
mailing list