Lightweight concurrency in Python
27 יולי, 2010 -
Yesterday, in a meeting of PyWeb-IL, Uriel Katz presented different options of asynchronous networking (and general event handling) in Python. His favorite of the bunch was gevent, which is based on greenlet.
Greenlet is specifically interesting because it allows very lightweight "concurrency" — in the form of coroutines. This means that there's no real concurrency, just "fake" concurrency (a function can let others run while it waits for something to happen), and the multitasking is cooperative. What you get for these limitations is very small consumption of system resources, compared with full-blown threads.
Python supports a form of coroutines out-of-the box, through generators, but they are not composable like greenlets (using a generator from a generator is cumbersome). Python also has a version that has lightweight threads with (optional) preemptive multitasking, called Stackless, but that involves patching the core interpreter. Greenlet, which is just a C extension for the normal interpreter, is an interesting middle ground between these two.
