<div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">def blockOn(d):<br> """<br> Use me in stacklessy-code to wait for a Deferred to fire.<br>
If the result is an failure, send the exception via the channel<br> to be captured by the tasklet.<br> """<br> ch = stackless.channel()<br> def cbOK(r):<br> ch.send(r)<br> def cbNOK(r):<br>
ch.send_exception(r.type, r.value)<br> d.addCallbacks(cbOK, cbNOK)<br> return ch.receive()</blockquote>
<div> </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 'good' and 'bad' from the example):</div>
<div> </div>
<div>def blockOn(df):<br> """<br> Use me in stacklessy-code to wait for a Deferred to fire.<br> If the result is an failure, send the exception via the channel<br> to be captured by the tasklet.<br>
"""<br> ch = NWChannel()<br> me = stackless.getcurrent()<br> df.addCallback(good, me, ch)<br> df.addErrback(bad, me, ch)<br> return ch.receive()<br><br>-Greg</div></div>