I've re-written an example of a simple program included in the new Stackless Python Tutorial (see Chapter 4) to use generators for task switching between multiple processes in plain python. The example in the tutorial was written once using threads and once using Stackless. My version uses neither and relies only on the included random library module and generators (the yield statement) which are built in to standard CPython (as of 2.4.3).

The code performs the same tasks as the given examples and does it way quicker than the threaded version but about half as fast as stackless. Most of the time spent is in preparing the micro threads. With 10000 processes, it takes almost as long to play a game of 10 turns as a game of 1000 turns. I've been working on it away from my home machine, so I haven't had access to the beautiful top (linux command line based system monitor) and I'm not able to see what the memory usage is, but I'm guessing if I didn't use classes, it'd be smaller. My goal (for tomorrow) is to move over to a functional implementation and see how that pans out.

I'm psyched because I'm actually starting to get some of the concurrency related concepts I hear thrown around at the higher levels. Things like switching, blocking, starvation, and continuations are kind of making sense. In the next few days I also hope together some "what did I learn?" type of thoughts on a separate projects page. Way way in the future I'll be applying some of this "micro-threads-with-generators" stuff to a kinda functional python web server I'm noodling with.

current version: haystack.py