[Stackless] Docs, samples for Stackless 3.0

Richard Emslie rxe at ukshells.co.uk
Sat Nov 29 00:43:02 CET 2003


Hi Phillip,

Some info can be found in the docstrings.  Try
>>> import stackless
>>> help(stackless.tasklet)
>>> help(stackless.channel)

FWIW I've attached a very simple example of a generator class which may
give some insight to tasklet/channels if you already understand generators.

Cheers,
Richard


On Fri, 28 Nov 2003, Jerkins, Phillip wrote:

> Hello, all!
>
> I'm totally new to the whole Stackless Python thing.  It looks EXTREMELY
> interesting - and useful - to me.
>
> However, I'm reading on the Stackless site that Stackless 3.0 uses new
> constructs called tasklets and continuations.  Try as I might, I can
> find neither example code nor documentation on these objects.  The best
> I found was a detailed walkthrough with continuations, that seems to be
> totally deprecated now.  And I'm not nearly good enough at C to look
> through the source and figure it out.
>
> Can someone post (or email directly to me) some information on how to
> use Stackless 3.0?  Or, at least, a link to some help?
>
> Thanks much!
>
> Phil Jerkins
> Phillip.Jerkins at MorganKeegan.com
> (901) 531-3473
>
>
>
>
>
>
>
> **************************************************************************************************
> Morgan Keegan & Co., Inc. DOES NOT ACCEPT ORDERS AND/OR
> INSTRUCTIONS REGARDING YOUR ACCOUNT BY E-MAIL.  Transactional details
> do not supersede normal trade confirmations or statements.  The information
> contained in this transmission is privileged and confidential. It is intended for the use of
> the individual or entity named above. The information contained herein is based on
> sources we believe reliable but is not considered all-inclusive. Opinions are our current
> opinions only and are subject to change without notice.  Offerings are subject to prior
> sale and/or change in price.  Prices, quotes, rates and yields are subject to change
> without notice.  Morgan Keegan & Co., Inc., member NYSE, NASD and SIPC, is a
> registered broker-dealer subsidiary of Regions Financial Corporation.  Investments are
> NOT FDIC INSURED, NOT BANK GUARANTEED and MAY LOSE VALUE.  Morgan
> Keegan & Co., Inc. reserves the right to monitor all electronic correspondence.
>
>
> http://www.morgankeegan.com
> **************************************************************************************************
>
-------------- next part --------------
from __future__ import generators
import stackless


#//////////////////////////////////////////////////////////////////////////

class Generator(object):
    
    def __init__(self, *args, **kwds):
        """ Generators without the syntactic sugar ;-) """

        self.inwards = stackless.channel()
        self.outwards = stackless.channel()

        task_factory = stackless.tasklet(self.generator_wrapper)
        self.task = task_factory(*args, **kwds)

        # Will run up first self.inwards.recv() - see yyy 
        self.task.run()
        

    def __iter__(self):
        return self

            
    def _yield(self, value):
        # next() has been called and waiting for a value 
        self.outwards.send(value)

        # Wait for the next next() - yyy2
        self.inwards.receive()


    def next(self):

        # Ask for yyy2 to unblock itself, the tasklet may die at this point
        self.inwards.send(None)
            
        if self.task.alive:
            return self.outwards.receive()

        else:
            raise StopIteration

        
    def generator_wrapper(self, *args, **kwds):
        
        # yyy
        self.inwards.receive()
        self.generator(*args, **kwds)


#//////////////////////////////////////////////////////////////////////////

def xRange(limit):
    """ xRange using generators """
    count = 0
    while count < limit:
        yield(count)
        count += 1


#//////////////////////////////////////////////////////////////////////////

class xRange2(Generator):
    """ Ditto with rolled our own generator. """   
    
    def generator(self, limit):
        count = 0
        while count < limit:
            self._yield(count)
            count += 1
            

#//////////////////////////////////////////////////////////////////////////

if __name__ == '__main__':

    print "For loop"
    for ii in xRange2(5):
        print ii


    print "While loop"
    x = iter(xRange2(3))
    try:
        while True:
            print x.next()

    except StopIteration:
        pass

    print "DONE"


More information about the Stackless mailing list