Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure python implementation of greenlet API

Tags:

The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn't work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.

A simple example:

def test1():
    print 12
    gr2.switch()
    print 34

def test2():
    print 56
    gr1.switch()
    print 78

gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()

Should print 12, 56, 34 (and not 78).

like image 290
Tristan Avatar asked May 30 '10 17:05

Tristan


People also ask

What is Python Greenlet?

Greenlets are a very lightweight coroutine written in C that are cooperatively scheduled. They provide us with a very lightweight thread- like object that allows us to achieve concurrent execution within our Python programs without incurring the cost of spinning up multiple threads.

What is the difference between Greenlet and gevent?

gevent is an open source project written and maintained by Denis Bilenko and is spread under the MIT license. Greenlet is a lightweight coroutine provided to Python as a C extension module. It is a primary pattern used in gevent that runs inside of the OS process for the main program.

Is Greenlet a thread?

Both green threads (greenlets) and POSIX threads (pthreads) are mechanisms to support multithreaded execution of programs. POSIX threads use the operating system's native ability to manage multithreaded processes.

What is gevent spawn?

The important parts of the program are the gevent. spawn which wraps up the given function inside of a Greenlet thread. The list of initialized greenlets are stored in the array threads which is passed to the gevent. joinall function which blocks the current program to run all the given greenlets.


1 Answers

This kind of thing can be achieved with co-routines which have been built-in to the standard Python distribution since version 2.5. If IronPython and co are fully compliant with all Python 2.5 features (I believe they are) you should be able to use this idiom.

See this post for more information on how they can be used :) Specifically, you'll be interested in the PDF where the author builds a system using nothing but pure Python that provides similar capabilities to either stackless Python or the Greenlet module.

You may also want to look either Gogen or Kamelia for ideas: these projects both have pure python coroutine implementations which you could either adopt or use as a reference for your own implementation. Take a look at this page for a gentle introduction to the cogen way of doing things.

Note there are some differences between the co-routine implementations here and the greenletimplementation. The pure python implementations all use some kind of external scheduler but the idea is essentially the same: they provide you with a way to run lightweight, co-operative tasks without the need to resort to threads. Additionally both the frameworks linked to above are geared towards asynchronous IO very much like greenlet itself.

Here's the example you posted but rewritten using cogen:

from cogen.core.coroutines import coroutine
from cogen.core.schedulers import Scheduler
from cogen.core import events

@coroutine
def test1():
    print 12
    yield events.AddCoro(test2)
    yield events.WaitForSignal(test1)
    print 34

@coroutine
def test2():
    print 56
    yield events.Signal(test1)
    yield events.WaitForSignal(test2)
    print 78

sched = Scheduler()
sched.add(test1)
sched.run()

>>> 12
>>> 56
>>> 34

It's a little more explicit than the greenlet version (for example using WaitForSignal to explicitly create a resume point) but you should get the general idea.

edit: I just confirmed that this works using jython

KidA% jython test.py 
12
56
34
like image 136
jkp Avatar answered Sep 19 '22 00:09

jkp