Stackless Python: programming the way Guido prevented it
Why Stackless is Cool Microthreads Generators Coroutines
Microthreads Very lightweight (can support thousands) Locks need not be OS resources Not for blocking I/O A comfortable model for people used to real threads
Generators A simple subcase of coroutines Often a more natural expression of an algorithm Replace a class / magic method with a function
Generator Example def fibonacci(): x, y = 0, 1 while 1: x, y = y, x+y suspend(y) Note: This example idealizes algorithmic clarity, but introduces a complexity on the calling side (fibonacci is a function object, while we want a generator object). This can be accommodated by having fibonacci() return a callable object that is then called to get successive results. Or fibonacci can stay as written, and the calling side can use a wrapper: fib = generator(fibonacci, ())
Coroutines Various ways to look at them Peer to peer subroutines Threads with voluntary swapping Generators on steroids (args in, args out) What’s so cool about them Both sides get to “drive” Often can replace a state machine with something more intuitive [1] [1] Especially where the state machine features complex state but relatively simple events (or few events per state).
Three Steps To Stacklessness Get Python data off the C stack Give each frame its own (Python) stackspace Get rid of interpreter recursions Result All frames are created equal Stack overflows become memory errors Pickling program state becomes conceivable
Getting rid of recursion is difficult Often there is “post” processing involved The C code (doing the recursing) may need its own “frame” Possible Approaches Tail optimized recursion Transformation to loop Either way, the “post” code needs to be separated from the “setup” code. Ironic Note: This is exactly the kind of pain we seek to relieve the Python programmer of!
Protecting against “illegal” transfers Can the extension (eg coroutines) provide the protection? Does the protection need to be in the Python core? Put another way: is thread / threading an acceptable model?
Language Support Can generators work naturally with __getitem__ ? Is any keyword support desirable (eg, suspend / resume)?