![]() |
|
SitemapMcMillan Enterprises, Inc.Python Pages Sockets HOWTO Distributing Python Programs A Python C++ API Embedding Python Stackless Python Getting Started with Stackless Fibonacci Generators Walking the Leaves of a Tree Parsing SelectDispatcher MkSQL Import Hooks Java Samples About ME Inc. |
What is StacklessNote: This is conceptual overview of the stackless implementation. It is not technically accurate. My intent is to give you just enough understanding to become dangerous, er, interested. Simply put, Stackless is a set of modifications to Python that keeps Python off the C stack. That is, the arguments and return addresses of your Python program are kept completely separate from the arguments and return addresses of the PVM (Python Virtual Machine). Not only that, but (with Stackless), Python no longer has a stack. Instead, each frame (the invocation of a function or method, or to put it another way, that which has locals) has its own little stacklette, (just large enough for its needs). Look at it this way: if you're using threads, each thread is given it's own stack. That's how calls, returns and arguments are kept straight. One thread can be swapped out and another swapped in because most of the "state" of a thread is resident on its stack. If nothing touches the stack while you're away, you're most of the way to picking up where you left off, just by switching stacks. (The other things you need to keep track of are the CPU's registers, including the all important instruction pointer.) So a Stackless frame has something in common with a thread - it can be put aside and picked up later. (That is a gross oversimplification, but we'll ignore that for now.) Stackless has an advantage over threads, though. Python knows exactly how much stack space will be required by a given frame when it compiles the code, and that's typically not much at all (we don't have to worry about the stack space called code will use, since it will get it's own stacklette). A thread, on the other hand, must assume the worst, and each thread is given as much stack space as possible (generally many megs - since the same stack will be used by called code). Now in normal Python, a frame is an ephemeral thing. They come into existence when a function or method is called, and disappear on return. The Python compiler figures out ahead of time everything it needs to know to create the frame efficiently at runtime, but the frame itself exists only as long as it (or a frame beneath it) is executing. This is the default case in Stackless, too. But extension modules can now change the rules of the game. Frames can be swapped in and out like threads, but much more cheaply (microthreads, or "green" threads). Frames can transfer control back to their caller and then get "called" again - picking up where they left off (generators). Or they can transfer off to someplace else, pick up where they left off but with an argument passed in - as though one side did a Of all of these, continuations are the most general and powerful (and dangerous). And this is what Christian has provided. You can build all the others from continuations. Will Ware has created a microthreads module. Someday soon there will be a coroutines module and perhaps a generator module (generators are easily done with coroutines, but don't need the extra syntax). The Tutorial pages that follow explain how to do generators and coroutines using the As the |
copyright 1999-2002 McMillan Enterprises, Inc. |