Howard Klein

Howard Klein

Musings on software, discrete event simulation and other pseudo-random topics

Python Coroutine Options, Part 2: Greenlets and Tasklets

In my last post, I discussed Python’s built-in support for coroutines via generators.  While we can use generators to implement simulation process code, the essentially stackless nature of generator functions limit their expressiveness and power.

Once we venture beyond Python’s standard library, there are a couple of other options that facilitate completely stackful coroutines: greenlets and tasklets.

Greenlets are part of the greenlet package, a third-party extension module for the standard CPython distribution.  Greenlets implement very lightweight microthreads, controlled through cooperative multitasking.  (Unlike operating system threads, which are quite heavyweight and may be pre-empted by the operating system at any time.)  A greenlet runs a function with its own stack, and that function (or any subroutine it calls) may yield by transferring control to another greenlet (via a switch() call).  When control is returned to the original greenlet, it resumes execution from the point where it left off.  From a purely ease-of-coding and natural expression point-of-view, greenlets are preferable to generators.

Tasklets are the central feature of Stackless Python, which is actually a standalone Python implementation. (The greenlet package is actually a spin-off of Stackless.) Functionally, Stackless Python is essentially a superset of the greenlet package; either can be used to implement fully stackful simulation process methods.. I actually implemented my initial simulation framework using Stackless and tasklets. The dependencies on Stackless were limited to just a couple of modules. It was fairly straightforward to swap out those modules with new versions using greenlet, while maintaining an identical API.

Finally, it should also be noted that yet another Python implementation, PyPy, includes support for both tasklets and greenlets.

In my final post in this series, I’ll do some compare and contrast around all of these options – generators, greenlets and tasklets – and how that led me to my ultimate choice for a simulation framework.  (Spoiler alert – it’s greenlets.)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>