[Twisted-Python] Returning a DeferredList?
terry at jon.es
Tue Jul 1 12:41:25 EDT 2008
>>>>> "Nathan" == Nathan <nathan.stocks at gmail.com> writes:
Nathan> Oh, ok. My apologies, Terry.
Thanks - but no need!
Nathan> After staring for awhile I think I figured out what's going on.
Nathan> First, everywhere I use this method in real code is to provide a
Nathan> final AMP response (which is why it works for me). I think what
Nathan> Jean Paul meant was that the AMP code looks at the object that the
Nathan> AMP responder returns, and if that object is a deferred, then the
Nathan> underlying AMP code (that you don't normally see) waits for the
Nathan> deferred's callback value and uses that as the actual return value
Nathan> of the AMP responder.
I've not looked at the code (maybe you should! :-)) but that sounds
right. And it would probably do this by just adding another callback to the
Nathan> I had mistakenly taken this as a more general method of
Nathan> returned-deferreds-resolving-into-their-callback-values, which is
Nathan> why everyone is taking issue with my hasty AMP-less pseudo-code.
That sounds right.
Nathan> I must say, I've had more trouble grasping the nuances of deferreds
Nathan> and other twisted objects than any other framework or language I've
Nathan> ever worked with. I also spend much more time _in vain_ going
Nathan> through the docs and api reference than other languages. At least
Nathan> there's a great mailing list!
I know the feeling. I've found it helpful to read the various Deferred
pages quite a few times through, to play with toy examples to see the flow
of control, and also to read the source of defer.py (which isn't complex
and which I think is elegant).
Nathan> So back to my original task -- I'm trying to get that same behaviour
Nathan> in AMP when using a deferred list -- but I could probably just create
Nathan> a deferred and return it as the AMP response (because AMP will wait
Nathan> for the deferred's callback value), and then fire a callback with my
Nathan> correct answer once I've processed my deferred list. Thus, I could
Nathan> avoid figuring out how AMP handles
Nathan> deferred-lists-returned-from-responders altogether.
That sounds right too. It might help to know that a DeferredList is just a
subclass of Deferred. So when you return a DeferredList, you're in fact
returning something that behaves like a regular Deferred. When its callback
is fired, it gets a list of status & results from all the deferreds in the
originally passed list. So if AMP knows how to handle deferreds, it can
certainly handle a DeferredList too.
Nathan> Have I got it straight now?
Yep, sounds like it.
More information about the Twisted-Python