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

Brian Granger bgranger at scu.edu
Thu Jan 12 00:29:05 EST 2006


On Jan 11, 2006, at 9:25 PM, Andrew Bennetts wrote:

> On Wed, Jan 11, 2006 at 08:52:02PM -0800, Brian Granger wrote:
>> 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?
>
> 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.
>

This is the case that I have.  This is what I did originally, but  
then I wanted to be fancy and use Deferred's.  Guess I will go back  
to using some version of a callable.

Thanks.

> 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.
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list