[Python-ideas] Throwing exceptions from inside coroutines.

Ron Adam ron3200 at gmail.com
Fri Nov 11 23:38:52 CET 2011


Idea...  Exceptions should be thrown out of coroutines rather than
raised inside them and allowed to propagate out.

There are several benefits.

* It doesn't stop or leave the coroutine in a state that can't be
continued.

* It maintains the boundary between coroutines and other code in such a
way that they may be replaced or wrapped in threads or processes easier.

* It creates a new way to communicate to outside framework which
simplifies creating schedulers.

* It would work with yield-from expressions to almost completely
eliminate any support framework like trampolines runners. (and the
cothread.py module here.)


My fist thoughts on this was to allow a resumable exception type, but
after thinking about it more, I don't think it would be a good idea.

Rather than try to explain this in words, here is a version of Nicks
example cothread.py module with a throws() function that can be used to
throw out an exception in a clean way.  This isn't meant to be included
in the library, but is a way to experiment with the API used to write
coroutines as Nick suggested.

Also here are two modules from Gregs examples that shows some context in
which these features are used.

Cheers,
   Ron

-------------- next part --------------
A non-text attachment was scrubbed...
Name: cothread.py
Type: text/x-python
Size: 3046 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111111/9db6714a/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sched_test.py
Type: text/x-python
Size: 1774 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111111/9db6714a/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: philosophers.py
Type: text/x-python
Size: 3426 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111111/9db6714a/attachment-0002.py>


More information about the Python-ideas mailing list