[Stackless] Thought it is easy to right tasks continually waiting for messages

Jeremy Stott tins.jeremy at gmail.com
Sat Jan 2 12:25:28 CET 2010

On Sat, Jan 2, 2010 at 11:35 PM, TOUMAN <touman at btinternet.com> wrote:

> Hi I am a newbi to stakless. I have read the documentaion specially the
> piece by grant olson.
> I want to create three tasks which are continually waiting for messages and
> don not die until they recieve the "exit" message. I have written the code
> below:
> import os, sys
> import stackless
> import math
> global _unit_A, _unit_B, _unit_C
> _unit_A = stackless.channel()
> _unit_B = stackless.channel()
> _unit_C = stackless.channel()
> class units:
>   def __init__(self,channel,name):
>     self.ch = channel
>     self.name = name
>     stackless.tasklet(self.process)()
>   def process(self):
>     print self.name,"Unit running"
>     while True:
>       msg = self.ch.receive()
>       if msg == "exit":
>         return
>       print self.name,":",msg
>       stackless.schedule()
> def testit():
>   print "Testing the parallel tasks"
>   units(_unit_A,"unit A")
>   units(_unit_B,"unit B")
>   units(_unit_C,"unit C")
>   msg = "Msg from unit A who says hello"
>   _unit_B.send(msg)
>   stackless.run()
> testit()
> But when I run it, it runs through all tasks once and the program finishes
> and I get this.
> c:\>
> Testing the parallel tasks
> unit A Unit running
> unit B Unit running
> unit B : Msg from unit A who says hello
> unit C Unit running
> c:\>
>  I am sure it must be something simple, but cant figure it out. Does
> anybody know what is happening here and what I am doing wrong?
> cheers
> tomcat
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless

Hi tomcat,

I am new to stackless myself, but in my experience, if all running tasklets
are waiting on channels then stackless.run() returns.

In your example, unitA, unitB and unitC are consumers waiting to consume on
the A,B,C channels. However, there are no producers for the A,B,C channels
(except for the main thread that sent one message to start with). When the
scheduler runs tasklets A, B, and C it ends up waiting on all three channels
infinitely. I suspect that stackless.run() returns so that you can handle
the dead-lock.

In practice, I would imagine that you would want to have a producer tasklet
that sends the exit commands based on some condition (keyboard press,
network signal etc). In this case, the scheduler would have something to
schedule while the other tasklets waited on channel A, B and channel C.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20100103/0ce59f5c/attachment.htm>

More information about the Stackless mailing list