Hi Andy,<br>Your example code is very close to working. The reason the chain didn&#39;t proceed as you expected is because the callbacks of the second <span style="font-family: courier new,monospace;">Deferred</span> created in the function <span style="font-family: courier new,monospace;">print_something</span> were never triggered to run.<br>
<br>You need to keep track of what actually triggers a deferred&#39;s callback chain to run and what each of the callback functions return. <br>In practice, that second deferred could have been replaced by some asynchronous function that actually returned a deferred, and your code would have worked.<br>
<br>The simplest change to make you code work is to change <span style="font-family: courier new,monospace;">print_something</span> to this:<br><br><span style="font-family: courier new,monospace;">def print_something(x):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;printing: &quot;, x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d = defer.Deferred()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d.addCallbacks(do_something_inbetween, oops)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d.callback(&quot;printed something&quot;)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return d</span><br>Doing this is kind of redundant though, because in this case it is equivalent to:<br><br><span style="font-family: courier new,monospace;">def print_something(x):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;printing: &quot;, x</span><span style="font-family: courier new,monospace;"></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return do_something_inbetween(&#39;printed_something&#39;)</span><br>
<br><br>Another thing you can do instead of creating a new deferred is wrap the call to <span style="font-family: courier new,monospace;">do_something_inbetween</span> with <span style="font-family: courier new,monospace;">defer.maybeDeferred</span>. This is convenient because if <span style="font-family: courier new,monospace;">do_something_inbetween</span> does not return a deferred (which is the case here), then <span style="font-family: courier new,monospace;">maybeDeferred</span> will do this for you. <br>
<br>Here&#39;s an example (a slight modification of your original) that includes calling a function that returns a <span style="font-family: courier new,monospace;">deferred</span> and using <span style="font-family: courier new,monospace;">maybeDeferred</span> to construct a deferred chain.<br>
<br><span style="font-family: courier new,monospace;"></span><br><span style="font-family: courier new,monospace;">from twisted.internet import reactor, defer</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">def do_something_inbetween(x):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;doing something else: &quot;, x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return &quot;done something else&quot;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def print_something(x):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;printing: &quot;, x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d = defer.maybeDeferred(do_something_inbetween,&#39;printed something&#39;)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d.addCallbacks(finish_up, oops)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return d</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def finish_up(x):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;finishing up with: &quot;, x</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def oops(e):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print str(e)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def go():</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &quot;constructed deferred&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; d = defer.Deferred()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return d</span><br><br><span style="font-family: courier new,monospace;">def extra(r):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print &#39;extra&#39;, r</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; return r</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">d = go()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">d.addCallback(print_something)</span><br>
<span style="font-family: courier new,monospace;">d.addCallback(extra)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">d.addErrback(oops)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">reactor.callLater(1, d.callback, &quot;hello mr&quot;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">reactor.callLater(5, reactor.stop)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">print &quot;setup everything&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">reactor.run()</span><br><br>Hope that helps.<br>
-Dorian<br><br><br><div class="gmail_quote">On Wed, Jun 18, 2008 at 11:39 PM, Andy Hird &lt;<a href="mailto:andyhird@gmail.com">andyhird@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br><br>I&#39;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&#39;m going wrong.<br><br>I&#39;ve attached the basic code to this message.<br>

<br>I&#39;d expect output to be:<br><br><br>constructed deferred<br>setup everything<br>printing:&nbsp; hello mr<br>doing something else: printed something<br>finishing up with: done something else<br><br>But instead the callback chain stops after print_something() has been called (do_something inbetween and finish_up are never called).<br>

<br>Thanks for any help in advance!<br><font color="#888888"><br>Andy Hird<br>
</font><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br>