[Twisted-Python] Returning a DeferredList?

Terry Jones terry at jon.es
Mon Jun 30 18:20:48 EDT 2008

>>>>> "Nathan" == Nathan  <nathan.stocks at gmail.com> writes:

Nathan> I regularly use the return values of deferreds to simulate
Nathan> synchronous behaviour like this:


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.



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])
   return dl

def test():
    def cb(val):
        print "retval is:", val

reactor.callLater(2.0, reactor.stop)

More information about the Twisted-Python mailing list