<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Hi Bin:</span></div><div><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><div class="y_msg_container">Message: 1<br>Date: Wed, 12 Jun 2013 16:31:50 -0600<br>From: Bin Huang <<a ymailto="mailto:bin.arthur@gmail.com" href="mailto:bin.arthur@gmail.com">bin.arthur@gmail.com</a>><br>To: <a ymailto="mailto:stackless@stackless.com" href="mailto:stackless@stackless.com">stackless@stackless.com</a><br>Subject: [Stackless] Difference between stackless.run() and<br>    tasklet.run()?<br>Message-ID:<br>    <CAJkdMQYgbc3CBgh-0OgE+igrh4hkD_97EqdnB+<a ymailto="mailto:XP38Ja55hCXg@mail.gmail.com"
 href="mailto:XP38Ja55hCXg@mail.gmail.com">XP38Ja55hCXg@mail.gmail.com</a>><br>Content-Type: text/plain; charset=ISO-8859-1<br><br><br><span style="font-size: 12pt;">>While I was learning the Stackless, I noticed a nuance between</span><br>>executing two pieces of code which are very similar.<br><br>>The difference between outputs is that the first code can exit from<br>>while(1) loop while the second code cannot. I suspect there is a<br>>difference between stackless.run() and tasklet.run() but I could not<br>>find any good documentation on this. I hope someone can give me some<br>>hints.</div><div class="y_msg_container"><br></div><div class="y_msg_container">stackless.run() starts the stackless scheduler. Think of stackless.run() being in an infinite</div><div class="y_msg_container">loop similar to an event loop for a UI framework. Like your second example, you set things</div><div class="y_msg_container">up then actually start
 things executing with a stackless.run()</div><div class="y_msg_container"><br></div><div class="y_msg_container">tasklet.run() places a tasklet at the front of the <span style="font-size: 12pt;">runnable queue and schedules it (i.e., it is running)</span></div><div class="y_msg_container"><span style="font-size: 12pt;">I would argue you use tasklet.run() if you are building some sort of custom scheduler.</span></div><div class="y_msg_container"><br></div><div class="y_msg_container">An important note is if the main tasklet (that exists at the beginning of the world) </div><div class="y_msg_container">terminates, any remaining tasklets will not be scheduled.</div><div class="y_msg_container"><br></div><div class="y_msg_container">I am going to rewrite your code to look like:</div><div class="y_msg_container"><br></div><div class="y_msg_container">def f():<br>      while 1:<br>              print
 id(stackless.current)<br>             stackless.schedule()<br><br>print "I am the main tasklet", id(stackless.current)<br>t1 = stackless.tasklet(f)()<br>t2 = stackless.tasklet(f)()<br>t3 = stackless.tasklet(f)()<br>t1.run()<br></div><div class="y_msg_container">print "Main tasklet done", id(stackless.current)</div><div class="y_msg_container"><br></div><div class="y_msg_container">In the case of the first example, what happens is your programme begins with a main tasklet. You </div><div class="y_msg_container">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. <span style="font-size: 12pt;">The call to stackless.schedule() causes the next tasklet to execute. Still</span></div><div class="y_msg_container"><span style="font-size: 12pt;">things are good. </span><span style="font-size: 12pt;">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. </span></div><div class="y_msg_container"><span style="font-size: 12pt;">Rather they were not scheduled.</span></div><div class="y_msg_container"><br></div><div class="y_msg_container">I hope this clears things up.</div><div class="y_msg_container"><br></div><div class="y_msg_container">Cheers,</div><div class="y_msg_container">Andrew</div> </div> </div>  </div></body></html>