[Twisted-Python] Repeatedly calling Deferred.callback()

Hi, 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? Thanks Brian

On Wed, 11 Jan 2006 20:52:02 -0800, Brian Granger <bgranger@scu.edu> wrote:
Nope, they're not. Twisted generally reverts to named method callbacks or explicit listener registration for cases where multiple results are possible. I've played with support code for simplifying this in the past, but it has never really resulting in something that was easier to use. Jean-Paul

On Wed, Jan 11, 2006 at 08:52:02PM -0800, Brian Granger wrote:
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 you need. I hope this helps, -Andrew.

Hi, Brian Granger wrote:
When in doubt, do both -- use a callback for each result, and then fire the Deferred when you're done. -- Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@smurf.noris.de Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de - - Exercise care in the abuse of oppressed peoples. Many farm implements make effective weapons in the hands of a skilled opponent. Some of those little old men can teach you a thing or two about hand-to-hand, too. -- The Legion Of Doom Troop Member's List

On Wed, 11 Jan 2006 20:52:02 -0800, Brian Granger <bgranger@scu.edu> wrote:
Nope, they're not. Twisted generally reverts to named method callbacks or explicit listener registration for cases where multiple results are possible. I've played with support code for simplifying this in the past, but it has never really resulting in something that was easier to use. Jean-Paul

On Wed, Jan 11, 2006 at 08:52:02PM -0800, Brian Granger wrote:
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 you need. I hope this helps, -Andrew.

Hi, Brian Granger wrote:
When in doubt, do both -- use a callback for each result, and then fire the Deferred when you're done. -- Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@smurf.noris.de Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de - - Exercise care in the abuse of oppressed peoples. Many farm implements make effective weapons in the hands of a skilled opponent. Some of those little old men can teach you a thing or two about hand-to-hand, too. -- The Legion Of Doom Troop Member's List
participants (4)
-
Andrew Bennetts
-
Brian Granger
-
Jean-Paul Calderone
-
Matthias Urlichs