[pypy-dev] Stacklets

Armin Rigo arigo at tunes.org
Thu Aug 11 18:46:45 CEST 2011


Re-hi,

On Thu, Aug 11, 2011 at 10:59 AM, Armin Rigo <arigo at tunes.org> wrote:
> Ah, I think it's what you get if you assume that your language has
> callcc but no exceptions, and explicitly implement exceptions on top
> of callcc.

Ok, after a fruitful discussion on IRC (thanks Da_Blitz), here's yet
another slightly different concept, almost as primitive as the
previous one, but capable of doing the obvious right thing with
exceptions.  It also has all the good properties of Python generators,
so let's call it "genlets" for now.  (For example, you can raise in a
Python generator, or resume a suspended generator in a different
thread, and the right thing occurs; this also occurs with genlets.)

Moreover, we can give it an interface very similar to generators.  A
genlet object has a primitive method send(x), a wrapper next() that
just calls send(None), and is an iterator.  Example:

@genlet
def f(gen, a, b):
    for i in range(a, b):
        gen.send(i)

for x in f(10, 20):
    print x

Of course this example is designed to show the similarity with
generators.  The 'gen' received by the generator as a first argument
is the same object as the one iterated over in the 'for' loop below;
this means more symmetrical usages are possible, too, using another
method gen1.switch(gen2, x) to do a "symmetrical switch" from gen1 to
gen2, as opposed to send() which does a going-into or getting-out-of
operation.

This looks like a "Pythonic" enough interface, just because it is very
similar to the existing one of generators.  It is also rather
primitive, although good enough to directly use like the example
above.  I can give a more precise definition of what send() and
switch() do, but I think I'll leave that for the next e-mail :-)  Does
the API presented here makes sense?


A bientôt,

Armin


More information about the pypy-dev mailing list