[Stackless] Threads and Tasklets

Richard richard at ccpgames.com
Mon Oct 24 10:20:31 CEST 2005

This is how I understand it...

> -----Original Message-----
> From: Andrew Francis
> I have been experimenting with stackless. I am
> not quite sure about the relationship between
> stackless tasks and Python/OS threads. I am under

Stackless tasklets are basically shifted in and off the C
stack as they become active and then yield (or exit).

Each thread has its own C stack.

So I believe you would need to have a separate scheduler
for each thread and the tasklets could only directly
interact with each other if they were on the same
thread/scheduler.  After all, you can't shift in part of
the C stack from one thread onto another.

While I may be wrong, I doubt you can have a separate
instance of the stackless module running in each thread
as I believe it implements a singleton of the scheduler.
You would have to have either one thread that does
stackless things and the rest as worker type threads or
a separate interpreter for each stackless instance I

> the impression that tasklets will block on system
> I/O. If I wish to do networking, can I use tasklets 
> to communicate with heavier weight threads, with 

If you want a tasklet to block while communicating with
something and the rest of the tasklets in the scheduler
to carry on being scheduled, I believe you have to write
code yourself that when called removes the current tasklet
keeps a reference to it and then reschedules it with a
return value whenever the blocking operation completes.

When it comes to the actual communication with threads
all I can do is hand-wave.  I don't use them myself in
EVE Online as all the wrapping of resources like networking
and database was already done for me when I started working
here years ago.  But I wouldn't expect it to be too much
work to do myself if I had the need.

You can always do non-blocking networking completely
in Python, you don't have to use threads.  Although
I understand the point of this post is to understand
tasklets and threads together :)

Here is a simple Python based MUD server that does
non-blocking networking.  It uses channels to stall a
specific tasklet for incoming data:

> constructs like queues? What are the gotchas? I would
> be happy to summarize on the blog.

My interpretation is that given you understand you have
to manage the tasklets that need to be suspended for
blocking operations yourself, once you take care of this
then all that is left is standard inter-thread

I hope this helps, sorry if I cannot be any help WRT
threads, but I haven't touched them since I started 
Python scripting at CCP.


Stackless mailing list
Stackless at stackless.com

More information about the Stackless mailing list