Cloud computing has led to the development of applications that need a very high level of concurrency. Objects and routines need to support the ability to suspend their state and then continue running at a later time. In cloud computing, it may even be necessary to continue on a different server than the one the routine began on. Python does support traditional threading but threading does not provide enough concurrency for many of today’s distributed applications. Fortunately, there is a solution: stackless Python.
What is it?
Stackless Python is an implementation of Python that does not use the “C” call stack. In traditional Python, the state of the interpreter and the currently executing code are both stored on the stack. This can affect concurrency. Additionally, the stack is limited to an area of memory. Stackless Python uses the heap which removes the memory limits associated with the stack.
You can download stackless Python from http://www.stackless.com. Typically you will need to install a separate stackless implementation of Python as not all extensions will be stable with the level of concurrency that stacless provides. Stackless provides a number of features to support high levels of concurrency.
Stackless Python provides microthreads in the form of objects called tasklets. A tasklet is a small, portable and lightweight task. In all stackless Python applications there is one main tasklet. Tasklets can be pickled and unpickled like other Python objects. This makes it possible to save and restore their state. Tasklets provide the concurrency in stackless Python and replace traditional threads.
Tasklets communicate with one another using channels. A channel is a bi-directional communication interface for tasklets. In stackless Python, a channel transmits an object between the sender and a receiving tasklet. Channels implement a queue so that multiple tasklets can be queued to send or receive. When an object is sent, the receiving tasklet is resumed. If no receiver is available to resume, the sender is suspended and queued. This is one of the mechanisms used in stackless Python to avoid blocking, a condition where a sender and receiver enter an infinite loop waiting on one another.
Stackless Python implements the concept of continuation. Essentially, continuation is when you save the state of a routine and then later restore that state, allowing the routine to continue running as though it had never been suspended. By extending continuation, you get the concept of coroutines. Greatly simplified, coroutines are continuations that call each other instead of themselves. Basically a coroutine is when a routine resumes another suspended routine. This can even take place over the network, allowing a routine on one server to resume a task running on another.
Finally, stackless Python includes a scheduler that keeps a list of tasklets and allows each one a turn to run. This scheduler provides for multitasking and concurrency. Since stackless Python does not use the C call stack, the overhead of moving running code on and off the stack is eliminated. This reduces the overhead associated with context switching under conventional threading models. As a result, stackless Python’s scheduler is very efficient.
The features of stackless Python make it an ideal platform for applications where a number of small autonomous tasks must interact with one another. Simulators are one example of this type of application. In a simulator, such as the one you might find in the Sims game, a number of autonomous processes are running at once. They perform some work and then sleep until another variable in the simulation requires them again. This is an ideal example of how an application may use continuation and coroutines. Agent based systems are another area where stackless Python makes sense.
The largest commercial use of stackless Python is the MMORPG EVE Online. EVE Online differs from other massively multiplayer online games in that its game world is not segemented into a series of game servers. Instead EVE runs a single game universe that is shared by all players. CCP, EVE’s creators, are large supporters and users of stackless Python and the EVE Online game is built with stackless Python. EVE, in particular, demonstrates that stackless Python is a viable choice for building highly concurrent, distributed applications. It seems that stackless Python would be an ideal platform for building cloud based applications.
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Programming in Python|
|Aug 02-04||Programming in Python|
|Oct 16-18||Programming in Python|
|- Classroom - Online|