Re: [Twisted-Python] Returning a DeferredList?
![](https://secure.gravatar.com/avatar/c8f4d4d1a8fc53c13ed05c202e0257fe.jpg?s=120&d=mm&r=g)
"Nathan" == Nathan <nathan.stocks@gmail.com> writes:
Nathan> I regularly use the return values of deferreds to simulate Nathan> synchronous behaviour like this: [snip] Nathan> So now I'm trying to do the same thing with DeferredList's, but Nathan> apparently the semantics aren't the same. Here's some actual Nathan> runnable sample code trying to use the DeferredList: Hi Nathan You're not using deferreds properly. In the simple/typical case, when you call a function that returns a deferred, you will want to add (at least) a callback to that deferred. The callback then gets called with the value that is being passed down the callback chain. Change your code to the below and it works. Your original code appears to work only because you're testing a deferred result in a Boolean context, which takes you to the true part of your if. You need to add call/errbacks to the deferred there too. Terry --- from twisted.internet import reactor, defer def callback_function(results): return "(--> This is the value that I want to get into retval! <--)" def make_a_list(): deferred1 = defer.Deferred() deferred2 = defer.Deferred() dl = defer.DeferredList([deferred1, deferred2]) dl.addCallback(callback_function) deferred1.callback('one') deferred2.callback('two') return dl def test(): def cb(val): print "retval is:", val make_a_list().addCallback(cb) reactor.callWhenRunning(test) reactor.callLater(2.0, reactor.stop) reactor.run()
![](https://secure.gravatar.com/avatar/826694d326649b5e681474e8e2116dc1.jpg?s=120&d=mm&r=g)
On Mon, Jun 30, 2008 at 4:20 PM, Terry Jones <terry@jon.es> wrote:
You're not using deferreds properly. In the simple/typical case, when you call a function that returns a deferred, you will want to add (at least) a
I don't think that's true. Jean-Paul was the one who told me to do it that way in the first place: http://twistedmatrix.com/pipermail/twisted-python/2008-April/017304.html If your theory was true, I would be getting garbage everywhere I return (and use) complicated objects, and I'd be getting True everywhere that I return booleans. Though you may be right for DeferredLists, which is why I'm asking here on the list... ~ Nathan
![](https://secure.gravatar.com/avatar/f9a435166977fabd5d9fdf4bba5d5459.jpg?s=120&d=mm&r=g)
On Mon, 30 Jun 2008 17:54:33 -0500, Nathan <nathan.stocks@gmail.com> wrote:
On Mon, Jun 30, 2008 at 4:20 PM, Terry Jones <terry@jon.es> wrote:
You're not using deferreds properly. In the simple/typical case, when you call a function that returns a deferred, you will want to add (at least) a
I don't think that's true. Jean-Paul was the one who told me to do it that way in the first place:
http://twistedmatrix.com/pipermail/twisted-python/2008-April/017304.html
Terry (hi Terry) is correct here. Your interpretation of Jean-Paul Calderone's message is flawed, although I certainly understand how the misinterpretation could occur. There's a big difference between the AMP example in that thread, and the example code you posted to this thread.
If your theory was true, I would be getting garbage everywhere I return (and use) complicated objects, and I'd be getting True everywhere that I return booleans.
Not necessarily. I suspect that your real code isn't working the way your posted example does. The bottom line is that the code you posted to this thread does not produce the synchronous behavior that you think it is providing, so it might be more helpful to provide an example of your real code, in order for people here to provide you with some better answers. Hope this helps, L. Daniel Burr
![](https://secure.gravatar.com/avatar/826694d326649b5e681474e8e2116dc1.jpg?s=120&d=mm&r=g)
On Mon, Jun 30, 2008 at 8:02 PM, L. Daniel Burr <ldanielburr@mac.com> wrote:
On Mon, 30 Jun 2008 17:54:33 -0500, Nathan <nathan.stocks@gmail.com> wrote:
On Mon, Jun 30, 2008 at 4:20 PM, Terry Jones <terry@jon.es> wrote:
You're not using deferreds properly. In the simple/typical case, when you call a function that returns a deferred, you will want to add (at least) a
I don't think that's true. Jean-Paul was the one who told me to do it that way in the first place:
http://twistedmatrix.com/pipermail/twisted-python/2008-April/017304.html
Terry (hi Terry) is correct here. Your interpretation of Jean-Paul Calderone's message is flawed, although I certainly understand how the misinterpretation could occur. There's a big difference between the AMP example in that thread, and the example code you posted to this thread.
Oh, ok. My apologies, Terry. After staring for awhile I think I figured out what's going on. First, everywhere I use this method in real code is to provide a final AMP response (which is why it works for me). I think what Jean Paul meant was that the AMP code looks at the object that the AMP responder returns, and if that object is a deferred, then the underlying AMP code (that you don't normally see) waits for the deferred's callback value and uses that as the actual return value of the AMP responder. I had mistakenly taken this as a more general method of returned-deferreds-resolving-into-their-callback-values, which is why everyone is taking issue with my hasty AMP-less pseudo-code. I must say, I've had more trouble grasping the nuances of deferreds and other twisted objects than any other framework or language I've ever worked with. I also spend much more time _in vain_ going through the docs and api reference than other languages. At least there's a great mailing list! So back to my original task -- I'm trying to get that same behaviour in AMP when using a deferred list -- but I could probably just create a deferred and return it as the AMP response (because AMP will wait for the deferred's callback value), and then fire a callback with my correct answer once I've processed my deferred list. Thus, I could avoid figuring out how AMP handles deferred-lists-returned-from-responders altogether. Have I got it straight now? ~ Nathan
participants (3)
-
L. Daniel Burr
-
Nathan
-
Terry Jones