[Stackless] Stackless and Twisted

Christian Tismer tismer at stackless.com
Tue Aug 7 22:29:10 CEST 2007


Hi Chris et al,

This is an old entry from March 4, 2004, and it seems to be identical
to http://twistedmatrix.com/trac/changeset/10072

Trying to get up-to-date, reading all stuff about Stackless
that I almost forgot, I found the ideas about twisted integration,
again.

It was mentioned from time to time, also different approaches
were tried.

Now I'm a bit confused if there is any consensus what a stackless/ 
twisted
integration should be, and if there are any problems left.

I'd be glad to hear your current optinion. Is this the way to go and is
just not used, or is it unsolved, or just a bad idea? This is without
any emotions, would be glad it it is just fine, also ok if not, but my
problem is that I don't have a clue :-)

greetings & cheers - chris

On Mar 4, 2004, at 4:06 AM, Christopher Armstrong wrote:

> Ok, here's a completely rewritten stackless support module for  
> Twisted. I used to have one that did all kinds of stupid  
> unnecessary stuff with the reactor and junk, but it's gone now. For  
> you CVS Twisted users, this is in sandbox/radix/threadless.py.
>
> It offers two functions: blockOn and deferredTasklet.
>
> deferredTasklet is a function decorator that you should use on your  
> stacklessy code that you want to integrate with Twisted.
>
> blockOn is a function that you should pass a Deferred to wait for  
> it to fire. Use it in your stacklessy-code.
>
> There's a TEST function (that's automatically run when you execute  
> the module) that shows this off with some somewhat helpful  
> comments. There are also prints sprinkled throughout the code to  
> show you what's going on.
>
> -- 
>  Twisted | Christopher Armstrong: International Man of Twistery
>   Radix  |          Release Manager,  Twisted Project
> ---------+           http://radix.twistedmatrix.com/
> import stackless
> from twisted.internet import defer
>
> def deferredTasklet(func):
>     """
>     I'm a function decorator that makes a stacklessy-function (one
>     that might 'block', etc) into one that returns a Deferred for
>     integrating with Twisted.
>     """
>     def replacement(*args, **kwargs):
>         d = defer.Deferred()
>         def tasklet(*args, **kwargs):
>             try:
>                 d.callback(func(*args, **kwargs))
>             except:
>                 d.errback()
>             print "hey, I just callbacked or errbacked."
>         print "task...", func.__name__
>         crap = stackless.tasklet(tasklet)(*args, **kwargs)
>         crap.run()
>         print "...let", func.__name__, crap
>         return d
>     return replacement
>
>
> def blockOn(d):
>     """
>     Use me in stacklessy-code to wait for a Deferred to fire.
>     XXX: If the result is an failure, raise its exception.
>     """
>     ch = stackless.channel()
>     def cb(r):
>         print "blockOnCB", r
>         ch.send(r)
>     d.addBoth(cb)
>     return ch.receive()
>
>
> def TEST():
>     """
>     Show off deferredTasklet and blockOn.
>     """
>     from twisted.internet import reactor
>
>     import time
>     #let's make sure we're not blocking anywhere
>     def timer():
>         print "time!", time.time()
>         reactor.callLater(0.5, timer)
>     reactor.callLater(0, timer)
>
>     def getDeferred():
>         d = defer.Deferred()
>         reactor.callLater(3, d.callback, 'goofledorf')
>         print "blocking on", d
>         r = blockOn(d)
>         print "got", r, "from blocking"
>         return r
>
>     getDeferred = deferredTasklet(getDeferred)
>
>
>     # below is our 'legacy' Twisted code that only knows about
>     # Deferreds, not crazy stackless stuff.
>
>     print "getDeferred is", getDeferred
>     d = getDeferred()
>     print "d is", d
>
>     def _cbJunk(r):
>         print "RESULT", r
>         reactor.stop()
>
>     d.addCallback(_cbJunk)
>     print "kicking it off!"
>     reactor.run()
>
>
> if __name__ == '__main__':
>     TEST()
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless

--
Christian Tismer             :^)   <mailto:tismer at stackless.com>
tismerysoft GmbH             :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56  mobile +49 173 24 18 776  fax +49 30 80 90 57 05
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20070807/8509fd7b/attachment.htm>
-------------- next part --------------
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://stackless.com/cgi-bin/mailman/listinfo/stackless


More information about the Stackless mailing list