[Twisted-Python] Re: Making DeferredList look like Deferred
Thomas HERVE therve at free.fr wrote:
The behaviour you want is provided by the option 'consumeErrors' of DeferredList (you can see why here :
http://twistedmatrix.com/projects/core/documentation/howto/defer.html#auto8). Aha, that's the piece I was missing! Thank you for pointing it out. 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'? Thanks, Lenny __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Quoting Lenny G Arbage <alengarbage@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
participants (2)
-
Lenny G Arbage
-
Thomas HERVE