[stdlib-sig] futures and deferreds

Antoine Pitrou solipsis at pitrou.net
Sat Nov 7 16:33:34 CET 2009


> They do seem specialized for continuation-passing style programming  
> though. As far as I can tell from the docs (http://python.net/crew/mwh/apidocs/twisted.internet.defer.Deferred.html 
> ), the only way to process the results of a Deferred is my installing  
> a callback.

Yes, but the last callback in the chain could trigger an Event, a Queue
or any other synchronization object to which you could wait on, if you
want something "synchronous" (despite the title of your original
message: "asynchronous execution" :-)).

> with ThreadedDeferredMaker(max_threads=5) as dm
>    deferreds = []
>    for url in URLS:
>      deferred = dm.defer(load_url, url)
>      deferred. addCallbacks(print_success, print_failure, url=url)
>      deferred.unpause()
>      deferreds.append(deferred)

If you have a bunch of deferreds and want your callback to trigger when
all deferreds are finished/failed, you can use a DeferredList. A
DeferredList is itself a Deferred so you can add callbacks to it
(including one which makes things synchronous as explained above).


(note that, as the doc says, "that you can still use a Deferred after
putting it in a DeferredList". That is, you can react individually to
each result to implement e.g. a progress indicator, and also react to
the completion of all deferreds)

Of course an all-synchronous API is still simpler to use for the use
cases it is meant for.



More information about the stdlib-sig mailing list