[pypy-dev] Improving Stackless/Coroutines implementation

Gabriel Lavoie glavoie at gmail.com
Tue Jun 29 21:09:40 CEST 2010

Hello everyone,
     as a few knows here, I'm working heavily with PyPy's "stackless" module
for my Master degree project to make it more distributed. Since I started to
work full time on this project I've encountered a few bugs (mostly related
to pickling of tasklets) and missing implementation details in the module.
The latest problem I've encountered is to be able to detect when
tasklet.kill() is called, within the tasklet being killed. With Stackless
CPython, TaskletExit is raised and can be caught but this part wasn't really
implemented in PyPy's stackless module. Since the module is implemented on
top of coroutines and since coroutine.kill() is called within
tasklet.kill(), the exception thrown by the coroutine implementation needs
to be caught. Here's the problem:




   Kill coro by sending an exception to it. (At the moment, the exception is
   not visible to app-level, which means that you cannot catch it, and that
   try: finally: clauses are not honored. This will be fixed in the future.)

The exception is not thrown at app level and a coroutine dies silently. Took
a look at the code and I've been able to expose a CoroutineExit exception to
app level on which I intend implementing TaskletExit correctly. I'm also
able to catch the exception as expected but the code is not yet complete.

Right now, I have a question on how to expose correctly the CoroutineExit
and TaskletExit exceptions to app level. Here's what I did:

W_CoroutineExit = _new_exception('CoroutineExit', W_Exception, 'Exit

class AppCoroutine(Coroutine): # XXX, StacklessFlags):

    def __init__(self, space, state=None):
        # Some other code here

        # Exporting new exception to __builtins__ and "exceptions" modules
        self.w_CoroutineExit = space.gettypefor(W_CoroutineExit)

I talked about this on #pypy (IRC) but people weren't sure about exporting
new names to __builtins__. On my side I wanted to make it look as most as
possible as how Stackless CPython did it with TaskletExit, which is directly
available in __builtins__. This would make code compatible with both
Stackless Python and PyPy's stackless module. Also, exporting names this way
would only make them appear in __builtins__ when the "_stackless" module is
enabled (pypy-c built with --stackless).

What are your opinions about it? (Maciej, I already know about yours! ;)

Thank you very much,

Gabriel (WildChild)

Gabriel Lavoie
glavoie at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20100629/846b5dff/attachment.html>

More information about the Pypy-dev mailing list