<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:14px">I'm trying to understand why a chain of Deferreds I'm dealing with hangs and why return values from some callbacks are not always making it to other callbacks down the chain of Deferreds.</span><div style="font-family:arial,sans-serif;font-size:14px">

<br></div><div style="font-family:arial,sans-serif;font-size:14px">Is it possible that there could be a race condition between <a href="http://twistedmatrix.com/documents/current/core/howto/defer.html" target="_blank">Deferred chaining and Deferred result gathering</a>?</div>

<div style="font-family:arial,sans-serif;font-size:14px">I.e., <a href="http://twistedmatrix.com/documents/current/core/howto/defer.html#auto13" target="_blank">Chaining Deferreds</a> says that "<i>If you need one Deferred to wait on another, all you need to do is return a Deferred from a method added to addCallbacks. Specifically, if you return Deferred B from a method added to Deferred A using A.addCallbacks, Deferred A's processing chain will stop until Deferred B's .callback() method is called; at that point, the next callback in A will be passed the result of the last callback in Deferred B's processing chain at the time</i>." </div>

<div style="font-family:arial,sans-serif;font-size:14px"><br></div><span style="font-family:arial,sans-serif;font-size:14px">Now at the same time, </span><font size="3" style="font-family:arial,sans-serif"><a href="http://twistedmatrix.com/documents/current/core/howto/defer.html#auto8" target="_blank">DeferredList</a> </font><span style="font-family:arial,sans-serif;font-size:14px">(on top of which gatherResults is built) warns that "</span><i style="font-family:arial,sans-serif;font-size:14px">If you want to apply callbacks to the individual Deferreds that go into the DeferredList, you should be careful about when those callbacks are added. The act of adding a Deferred to a DeferredList inserts a callback into that Deferred ... The important thing to remember is that it is this callback which records the value that goes into the result list handed to the DeferredList's callback.</i><div style="font-family:arial,sans-serif;font-size:14px">

<i>Therefore, if you add a callback to the Deferred after adding the Deferred to the DeferredList, the value returned by that callback will not be given to the DeferredList's callback. To avoid confusion, <b>we recommend not adding callbacks to a Deferred once it has been used in a DeferredList</b></i>"</div>

<div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">Given these two explanations, say I have something like this:</div><div style="font-family:arial,sans-serif;font-size:14px">

---------------------------------------------------------------------------<br></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">def slowFuncReturningDeferred_A():</font></div>

<div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">  ...</font></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace"><br></font></div>

<div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">def slowFuncReturningDeferred_B():<br></font></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">  ...</font></div>

<div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace"><br></font></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">d = defer.Deferred()</font></div>

<div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">d.addCallback(labmda _: slowFuncReturningDeferred_A())</font></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">d.addCallback(labmda _: slowFuncReturningDeferred_B())</font></div>

<div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace"><br></font></div><div style="font-family:arial,sans-serif;font-size:14px"><font face="courier new, monospace">defer.gatherResults([d])</font></div>

<div style="font-family:arial,sans-serif;font-size:14px">---------------------------------------------------------------------------<br></div><div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">

Wouldn't it be possible that <span style="font-family:'courier new',monospace">defer.gatherResults</span> inserts its callback into <span style="font-family:'courier new',monospace">d</span> before say <span style="font-family:'courier new',monospace">slowFuncReturningDeferred_B</span> gets to run and return its own Deferred (and therefore chain it to what gatherRestults is ultimately waiting for?). If so, wouldn't the results returned by <span style="font-family:'courier new',monospace">slowFuncReturningDeferred_B</span> never make it to the results gathered by <span style="font-family:'courier new',monospace">defer.gatherResults</span>?</div>

<div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">Thanks,</div><div style="font-family:arial,sans-serif;font-size:14px">Jorge</div>
</div>