[Python-Dev] Stackless Python - Pros and Cons

Christian Tismer tismer@appliedbiometrics.com
Mon, 07 Aug 2000 16:50:42 +0200


Jack Jansen wrote:
> 
> > > Let me make sure I understand: If I invoke a continuation when there
> > > are extra C stack frames between the mainloop invocation that captured
> > > the continuation and the call of the continuation, the interpreter
> > > raises an exception?
> >
> > Not always. Frames which are not currently bound by an
> > interpreter acting on them can always be jump targets.
> > Only those frames which are currently in the middle of
> > an opcode are forbidden.
> 
> And how about the reverse? If I'm inside a Python callback from C code, will
> the Python code be able to use continuations? This is important, because there
> are a lot of GUI applications where almost all code is executed within a C
> callback. I'm pretty sure (and otherwise I'll be corrected within
> milliseconds:-) that this is the case for MacPython IDE and PythonWin (don't
> know about Idle).

Without extra effort, this will be problematic. If C calls back
into Python, not by the trampoline scheme that stackless uses,
but by causing an interpreter recursion, then this interpreter
will be limited. It can jump to any other frame that is not held
by an interpreter on the C stack, but the calling frame of the
C extension for instance is locked. Touching it causes an
exception.
This need not necessarily be a problem. Assume you have one or a
couple of frames sitting around, caught as a continuation.
Your Python callback from C jumps to that continuation and does
something. Afterwards, it returns to the C callback.
Performing some cycles of an idle task may be a use of such
a thing.
But as soon as you want to leave the complete calling chain,
be able to modify it, return to a level above your callback
and such, you need to implement your callback in a different
way.
The scheme is rather simple and can be seen in the stackless
map implementation: You need to be able to store your complete
state information in a frame, and you need to provide an
execute function for your frame. Then you return the magic
Py_UnwindToken, and your prepared frame will be scheduled
like any pure Python function frame.

Summary: By default, C extensions are restricted to stackful
behaviror. By giving them a stackless interface, you can
enable it completely for all continuation stuff.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com