[Twisted-Python] Repeatedly calling Deferred.callback()
andrew-twisted at puzzling.org
Thu Jan 12 00:25:41 EST 2006
On Wed, Jan 11, 2006 at 08:52:02PM -0800, Brian Granger wrote:
> Are Deferred objects designed to have their callback method called
> multiple times it there are a multiple (discrete) results that arrive
> asynchronously? If not, what is the proper construct in Twisted for
> returning multiple asynchronous results from a single method?
Deferred objects are one-shot by design, so they are not by themselves suitable
for multiple asynchronous results.
If you have a fixed number of asynchronous operations that want to return
results, you could just return a Deferred per expected result. Callers that
wish to aggregate those can use a DeferredList.
If you have something that's more like an event source -- you don't know how
many results to expect in advance, you are probably better off with callable
that is invoked each time, and dispatches the result to whatever code is
appropriate at the time. Quite possibly a state machine written as a class with
different methods that will be invoked for the different kinds of events/results
you expect is appropriate.
If you have something that's conceptually one operation, but the results trickle
in in parts, then perhaps a hybrid approach is suitable. e.g. consider
downloading a large web-page: somewhere a dataReceived handler is getting
invoked many times, and this can be used to e.g. update a progress bar in a UI
somewhere, but once the entire page has been downloaded then a complete result
can be passed to a Deferred for code that only cares about the final result.
There are probably lots of other variations as well, depending on exactly what
I hope this helps,
More information about the Twisted-Python