[Stackless] Difference between stackless.run() and tasklet.run()?

Andrew Francis andrewfr_ice at yahoo.com
Fri Jun 14 19:37:44 CEST 2013

Hi Bin:

Message: 1
Date: Wed, 12 Jun 2013 16:31:50 -0600
From: Bin Huang <bin.arthur at gmail.com>
To: stackless at stackless.com
Subject: [Stackless] Difference between stackless.run() and
    <CAJkdMQYgbc3CBgh-0OgE+igrh4hkD_97EqdnB+XP38Ja55hCXg at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

>While I was learning the Stackless, I noticed a nuance between
>executing two pieces of code which are very similar.

>The difference between outputs is that the first code can exit from
>while(1) loop while the second code cannot. I suspect there is a
>difference between stackless.run() and tasklet.run() but I could not
>find any good documentation on this. I hope someone can give me some

stackless.run() starts the stackless scheduler. Think of stackless.run() being in an infinite
loop similar to an event loop for a UI framework. Like your second example, you set things
up then actually start things executing with a stackless.run()

tasklet.run() places a tasklet at the front of the runnable queue and schedules it (i.e., it is running)
I would argue you use tasklet.run() if you are building some sort of custom scheduler.

An important note is if the main tasklet (that exists at the beginning of the world) 
terminates, any remaining tasklets will not be scheduled.

I am going to rewrite your code to look like:

def f():
      while 1:
              print id(stackless.current)

print "I am the main tasklet", id(stackless.current)
t1 = stackless.tasklet(f)()
t2 = stackless.tasklet(f)()
t3 = stackless.tasklet(f)()

print "Main tasklet done", id(stackless.current)

In the case of the first example, what happens is your programme begins with a main tasklet. You 
create three additional tasklets. So far so good. You call t1.run().  Tasklet t1 is placed to the fron tof the queue and is executed. The call to stackless.schedule() causes the next tasklet to execute. Still
things are good. Finally the main tasklet is scheduled. The main tasklet simply terminates. And because the main tasklet terminated, the other tasklets don't exit from the infinite loop. 
Rather they were not scheduled.

I hope this clears things up.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20130614/77ae28dd/attachment.html>

More information about the Stackless mailing list