[Stackless] How tasklets are scheduled

Stephan Diehl stephan.diehl at gmx.net
Fri Aug 29 12:11:27 CEST 2003


I played around with stackless a little bit more. I made some observations 
about scheduling which I like to share here. It's probably stuff, everybody 
knows, but me :-)
Please correct me, if the description is wrong.

Stackless has an internal list of running tasklets. The main tasklet (the 
program that is executed) is always on that list.
In order to get a new tasklet on that list, its 'run' method must be called.
The tasklet is removed from that list, if it calls 'schedule()', i.e. that it 
will only continue to run, if its 'run' method is called again.

The following testcode shows this:
---------------------------------------------------------------------------------------------------------------
from stackless import *

def f(id):
    for i in range(7):
        print id,':',i
        schedule()
    schedule()

tasklist = []
for i in range(3):
    t = tasklet(f)(i)
    print 'creating',t
    tasklist.append(t)

while tasklist:
    for t in tasklist[:]:
        try:
            t.run()
        except RuntimeError: # see A
            print 'removing',t
            tasklist.remove(t)
        try:
            schedule()
        except SystemError:pass # see B

# A: if a tasklet has finished, running it
#      will result in a RuntimeError
# B: for some strange reasons, the last remaining
#      tasklet is not cleaned up properly (is this a bug?)
---------------------------------------------------------------------------------------------------------------

Instead of the 'run' method, 'insert' could be used as well. But this gives a 
different runtime behaviour.
(maybe 'insert' should be called 'append' since it appends the tasklet to the 
queue)

Does this make sense?

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




More information about the Stackless mailing list