[Twisted-Python] txdlo - a Twisted DeferredListObserver

Terry Jones terry at jon.es
Sun Dec 29 22:24:37 MST 2013


I just wrote a quick class called DeferredListObserver that lets you do
various things with a list of deferreds. You can add observers that get
passed information about the deferreds firing. You can also add deferreds
to the observed list at any time (this is very useful if you're dynamically
creating deferreds that you want to monitor).

The class can be used to easily build things like Twisted's DeferredList or
simple variants of it, or can let you separate the various behaviors of
DeferredList into simpler functions. You can also do other things that I've
occasionally wanted.  E.g., get a deferred that fires when N of the
observed deferreds have fired. Or ignore errors until one deferred
succeeds, only firing with an error if all deferreds fail. Or write a
DeferredPool with a notifyWhenEmpty method like the one I posted here about
4 years ago.

Or (a more involved example), suppose you have 3 methods that can return
you a user's avatar: a fast local cache, a filesystem, and a slow network
call to Gravatar. You want to launch all three lookups at once and use the
first answer. But if the cache and/or filesystems fails first, you don't
want an error you instead want to take the result from Gravatar and add it
to the cache and/or filesystem, as well firing a deferred with the result
(wherever it comes from). Only if all three lookups fail do you want to
receive an error.

I'll improve the examples (and probably add tests for them) at some point.
Meanwhile, the DeferredListObserver class (in txdlo.py), tests for it, and
various examples can be had from https://github.com/terrycojones/txdlo

Terry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20131230/ecc19210/attachment.html>


More information about the Twisted-Python mailing list