[Twisted-Python] Re: Making DeferredList look like Deferred

Thomas HERVE therve at free.fr
Mon Feb 6 15:35:27 EST 2006


Quoting Lenny G Arbage <alengarbage at yahoo.com>:

>  Now, though, this leads me to one more question:
> what if I want to wait until all the Deferreds have
> fired (either err or callback), and only then call the
> callback if all callbacks were fired, otherwise call
> the errback?
>  To accomplish this, I'm currently just 'wrapping'
> the DeferredList in a regular Deferred.  It's callback
> is always called on completion, so when it fires I
> look at all the results given to the callback, and if
> any have failed, raise an exception.  Otherwise I
> return a result.
>  This feels a little bit clunky, but I don't have a
> good suggestion for improving it --
> 'fireOnErrbackAfterAllCompleted=True'?

Well that's near the default behaviour with fireOnOneErrback=False. 
Don't forget
you have the status of each requests in the result list. You could something
like this :

d = DeferredList(myList)
d.addCallback(mycb)

def mycb(results):
    ret = []
    # Warning clumsy code
    for code, res in results:
        if code == defer.FAILURE: # or 'not code'
             return failure.Failure(res)
        else: # defer.SUCCESS
             ret.append(res)
     return ret

But really, I don't see the point of this. If you want to have an exception
somewhere, use fireOnOneErrback. Else there's no need to build the 
result list,
if you're actually raising an Exception for *one* error.

-- 
Thomas






More information about the Twisted-Python mailing list