[pypy-dev] Improving Stackless/Coroutines implementation

Gabriel Lavoie glavoie at gmail.com
Thu Jul 1 20:57:43 CEST 2010

Hello everyone,
     the change is implemented in r75735. I also added a coroutine.throw()
method to raise any exception inside any coroutine. I don't know if some
people need this but I personnally do. For now, it's implemented
approximately like greenlet.throw(). The documentation for stackless.html
page was updated in pypy/doc/stackless.txt. If someone could review the
changes and possibly update the documentation on the website it would be
appreciated. ;)


2010/6/29 Gabriel Lavoie <glavoie at gmail.com>

> 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:
> http://codespeak.net/pypy/dist/pypy/doc/stackless.html#coroutines
>    -
>    coro.kill()
>    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
> requested...')
> 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)
>         space.setitem(
>                       space.exceptions_module.w_dict,
>                       space.new_interned_str('CoroutineExit'),
>                       self.w_CoroutineExit)
>         space.setitem(space.builtin.w_dict,
>                       space.new_interned_str('CoroutineExit'),
>                       self.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

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

More information about the Pypy-dev mailing list