[Stackless] Stackless + Twisted with deferred operations

Greg Hazel greg at bittorrent.com
Mon Mar 12 22:17:46 CET 2007


>
> def blockOn(d):
>    """
>    Use me in stacklessy-code to wait for a Deferred to fire.
>    If the result is an failure, send the exception via the channel
>    to be captured by the tasklet.
>    """
>    ch = stackless.channel()
>    def cbOK(r):
>        ch.send(r)
>    def cbNOK(r):
>        ch.send_exception(r.type, r.value)
>    d.addCallbacks(cbOK, cbNOK)
>    return ch.receive()


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))
Using NWChannel, you can make a more robust blockOn like this (with 'good'
and 'bad' from the example):

def blockOn(df):
   """
   Use me in stacklessy-code to wait for a Deferred to fire.
   If the result is an failure, send the exception via the channel
   to be captured by the tasklet.
   """
   ch = NWChannel()
   me = stackless.getcurrent()
   df.addCallback(good, me, ch)
   df.addErrback(bad, me, ch)
   return ch.receive()

-Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20070312/38eee118/attachment.htm>
-------------- next part --------------
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless


More information about the Stackless mailing list