[Stackless] Example and doubts about the functioning of stackless

Carlos Eduardo de Paula cedepaula at yahoo.com.br
Thu Aug 24 16:49:46 CEST 2006


Hi all,

I´m still playing with stackless until I get a project to use it. I created a small app to perform a task and display a progressbar, each on its own tasklet.

My questions are:

- When I run the task in the doStuff alone(without the progressbar) the task is completed in about 1.06 seconds in my machine, and when I run it with the PB, it takes 2.17 seconds.

- When the task is finished it sends a signal to the PB so it stops, but the app stays running (i think its the ManageSleepingTasklets tasklet, how can i make it exit and finish?

- When the task is finished and the app is still running(see above), my CPU stays at 100%. What can I do to make my app not use all CPU?

Please, tell me if i´m doing anything wrong in it...

---------------------------------
import stackless
import time
import sys

##########################################################
sleepingTasklets = []

def Sleep(secondsToWait):
    channel = stackless.channel()
    endTime = time.time() + secondsToWait
    sleepingTasklets.append((endTime, channel))
    sleepingTasklets.sort()
    # Block until we get sent an awakening notification.
    channel.receive()

def ManageSleepingTasklets():
    while 1:
        if len(sleepingTasklets):
            endTime = sleepingTasklets[0][0]
            if endTime <= time.time():
                channel = sleepingTasklets[0][1]
                del sleepingTasklets[0]
                # We have to send something, but it doesn't matter what as it is not used.
                channel.send(None)             
        stackless.schedule()

stackless.tasklet(ManageSleepingTasklets)()


class progressBar(object):
    def __init__(self):
        p = stackless.tasklet(self.runpb)()
        #f = stackless.tasklet(func)()
        self.running = 1

    def runpb(self):
        cont = 0
        prog = ['-','\\','|','/']
        while self.running:
            print "Progress: [" , prog[cont] , "]\r",
            if cont <3: cont += 1
            else: cont = 0
            Sleep(0.1)
        else:
            print "Finished"

def doStuff(mult,pb=None):
    st = time.time()
    c = 0
    for i in xrange(int(1000000*mult)):
        c = c + 1
        stackless.schedule()
    if pb: pb.running = 0
    print "Took: " , time.time() - st , " seconds."


stackless.tasklet(doStuff)(1,progressBar()) # With progressbar
#stackless.tasklet(doStuff)(1)               # Without progressbar

stackless.run()

---------------------------------



Thanks for all,

Carlos





_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless


More information about the Stackless mailing list