[Twisted-Python] chaining deferreds
Hi all, I've been trying to get a really basic example of chaining deferreds going with no luck. I was hoping someone could give me some help and tell me where I'm going wrong. I've attached the basic code to this message. I'd expect output to be: constructed deferred setup everything printing: hello mr doing something else: printed something finishing up with: done something else But instead the callback chain stops after print_something() has been called (do_something inbetween and finish_up are never called). Thanks for any help in advance! Andy Hird
Hi Andy, Your example code is very close to working. The reason the chain didn't proceed as you expected is because the callbacks of the second Deferredcreated in the function print_something were never triggered to run. You need to keep track of what actually triggers a deferred's callback chain to run and what each of the callback functions return. In practice, that second deferred could have been replaced by some asynchronous function that actually returned a deferred, and your code would have worked. The simplest change to make you code work is to change print_something to this: def print_something(x): print "printing: ", x d = defer.Deferred() d.addCallbacks(do_something_inbetween, oops) d.callback("printed something") return d Doing this is kind of redundant though, because in this case it is equivalent to: def print_something(x): print "printing: ", x return do_something_inbetween('printed_something') Another thing you can do instead of creating a new deferred is wrap the call to do_something_inbetween with defer.maybeDeferred. This is convenient because if do_something_inbetween does not return a deferred (which is the case here), then maybeDeferred will do this for you. Here's an example (a slight modification of your original) that includes calling a function that returns a deferred and using maybeDeferred to construct a deferred chain. from twisted.internet import reactor, defer def do_something_inbetween(x): print "doing something else: ", x return "done something else" def print_something(x): print "printing: ", x d = defer.maybeDeferred(do_something_inbetween,'printed something') d.addCallbacks(finish_up, oops) return d def finish_up(x): print "finishing up with: ", x def oops(e): print str(e) def go(): print "constructed deferred" d = defer.Deferred() return d def extra(r): print 'extra', r return r d = go() d.addCallback(print_something) d.addCallback(extra) d.addErrback(oops) reactor.callLater(1, d.callback, "hello mr") reactor.callLater(5, reactor.stop) print "setup everything" reactor.run() Hope that helps. -Dorian On Wed, Jun 18, 2008 at 11:39 PM, Andy Hird <andyhird@gmail.com> wrote:
Hi all,
I've been trying to get a really basic example of chaining deferreds going with no luck. I was hoping someone could give me some help and tell me where I'm going wrong.
I've attached the basic code to this message.
I'd expect output to be:
constructed deferred setup everything printing: hello mr doing something else: printed something finishing up with: done something else
But instead the callback chain stops after print_something() has been called (do_something inbetween and finish_up are never called).
Thanks for any help in advance!
Andy Hird
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Andy Hird -
Dorian Raymer