[Python-Dev] Coroutines and PEP 380
mark at hotpy.org
Tue Jan 17 23:03:45 CET 2012
Lets start controversially: I don't like PEP 380, I think it's a kludge.
I think that CPython should have proper coroutines, rather than add more
bits and pieces to generators in an attempt to make them more like
I have mentioned this before, but this time I have done something about
I have a working, portable, (asymmetric) coroutine implementation here:
Its all standard C, no messing with the C stack, just using standard
techniques to convert recursion to iteration
(in the VM not at the Python level) and a revised internal calling
convention to make CPython stackless:
Then I've added a Coroutine class and fiddled with the implementation of
YIELD_VALUE to support it.
I think the stackless implementation is pretty solid, but the
coroutine stuff needs some refinement.
I've not tested it well (it passes the test suite, but I've added no new
It is (surprisingly) a bit faster than tip (on my machine).
There are limitations: all calls must be Python-to-Python calls,
which rules out most __xxx__ methods. It might be worth special casing
__iter__, but I've not done that yet.
To try it out:
>>> import coroutine
To send a value to a coroutine:
where co is a Coroutine()
To yield a value:
send() is a method, co_yield is a function.
Here's a little program to demonstrate:
def __init__(self, l, item, r):
self.l = l
self.item = item
self.r = r
if n == 0:
return Node(None, n, None)
return Node(make_tree(n-1), n, make_tree(n-1))
def walk_tree(t, f):
if t is not None:
co = coroutine.Coroutine(tree_yielder, (make_tree(2),))
Which will output:
Traceback (most recent call last):
File "co_demo.py", line 30, in <module>
TypeError: can't send to a halted coroutine
More information about the Python-Dev