<div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">def blockOn(d):<br>&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp; Use me in stacklessy-code to wait for a Deferred to fire.<br>
&nbsp;&nbsp; If the result is an failure, send the exception via the channel<br>&nbsp;&nbsp; to be captured by the tasklet.<br>&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp; ch = stackless.channel()<br>&nbsp;&nbsp; def cbOK(r):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch.send(r)<br>&nbsp;&nbsp; def cbNOK(r):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch.send_exception(r.type, r.value)<br>&nbsp;&nbsp; d.addCallbacks(cbOK, cbNOK)<br>&nbsp;&nbsp; return ch.receive()</blockquote>
<div>&nbsp;</div>
<div>The problem with this, and the purpose for NWChannel, is that d.addCallbacks() could call either of the callbacks immediately, causing a block before ch.reveive() is run. Try it: blockOn(defer.succeed(1))</div>
<div>Using NWChannel, you can make a more robust blockOn like this (with &#39;good&#39; and &#39;bad&#39; from the example):</div>
<div>&nbsp;</div>
<div>def blockOn(df):<br>&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp; Use me in stacklessy-code to wait for a Deferred to fire.<br>&nbsp;&nbsp; If the result is an failure, send the exception via the channel<br>&nbsp;&nbsp; to be captured by the tasklet.<br>
&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp; ch = NWChannel()<br>&nbsp;&nbsp; me = stackless.getcurrent()<br>&nbsp;&nbsp; df.addCallback(good, me, ch)<br>&nbsp;&nbsp; df.addErrback(bad, me, ch)<br>&nbsp;&nbsp; return ch.receive()<br><br>-Greg</div></div>