[Stackless] Just Not Getting One Aspect of Tasklets and Channel Receive on Pong Example

Christian Tismer tismer at stackless.com
Mon Mar 15 22:43:59 CET 2010

On 3/15/10 7:03 PM, Carl, Andrew F (AS) wrote:
> Christian,
>                 Thanks for the reply and link to the stackless 
> documentation. Seemed like there should be more than just the PDF. 
> Regarding your reply, the part which is unclear is the implication of 
> a tasklet which contains a channel receive. I understand the 
> functionality and blocking, but it would seem that 
> "stackless.tasklet(...)()" places an instance of a "callable" on the 
> stackless queue. But once the tasklet channel send is accomplished, 
> that tasklet would be complete and exit out of the queue, no longer 
> available to channel receive. It is for this reason that it appears 
> that a new tasklet is being placed in the queue as a result of a 
> channel send.

Why are you thinking that? The channel send is inside a function, and
when the send is done, the function continues. There is nothing
special here, and if it were, then it would surely be documented.

A tasklet is just a little housekeeping, wrapped around a piece
of code, and that is Python code that obeys the rules of Python.
And that piece of code can only be left by an explicit or
implicit return statement, or by an exception.
Tasklets are mostly like functions. When a tasklet goes asleep
on a channel, its current state is saved, so that it can
resume later on at exactly the same place where it left.

And a channel is just a little structure that holds a queue
of waiting tasklets, blocked either for sending or receiving.

The only unusual thing here is that the function resumes some
time later, when it gets unblocked from the channel. So the
only magic lies in the transfer of control during a channel
action. The time of 'what runs when' is what changes. The
rest is ordinary Python.

Ah, maybe I get the misunderstanding now. Yes, stackless.tasklet()
needs a callable, which gets called with another (), which creates
the instance of a callable, also called Python frame, and it is this
frame that the tasklet holds*). The frame itself is not callable,
this is only for the beginning of the tasklet's lifetime. After
that, it is just a resumable frame.
IOW. what Stackless essentially does is changing the order
of execution of these frames in a deterministic way.
I hope this paragraph did not add to confusion.

You can take my little walk-through literally for true,
this is exactly how it works, not more, not less.

Trust me, I initially wrote it, 10 years ago :-)

ciao -- chris

Please don't read this footnote before the above is sunken in!
*) well, in the case of tasklet( channel.send ) it is slightly
different, since channel.send is a builtin c function, and that
creates a different, but similar behaving, c-frame.
And that c-frame, bottom of our example, really ends after
being called, because there is nothing left after that call.
Forget about that detail, I just wanted to be correct.

Christian Tismer             :^)<mailto:tismer at stackless.com>
tismerysoft GmbH             :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key ->  http://wwwkeys.pgp.net/
work +49 30 802 86 56  mobile +49 173 24 18 776  fax +49 30 80 90 57 05
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20100315/9a4b1414/attachment.htm>

More information about the Stackless mailing list