# HG changeset patch # Parent 661cdbd617b8c1c6bccec1c967bc8ce5c570ad87 diff -r 661cdbd617b8 -r ea1cf086f4f4 Doc/glossary.rst --- a/Doc/glossary.rst Thu Apr 30 08:08:13 2015 -0700 +++ b/Doc/glossary.rst Thu Apr 30 21:20:31 2015 +0100 @@ -146,6 +146,11 @@ statement by defining :meth:`__enter__` and :meth:`__exit__` methods. See :pep:`343`. + coroutine + A generator function that works along with an event loop or trampoline + function to allow it to yield control to other generators without + blocking. See :pep:`342`. + CPython The canonical implementation of the Python programming language, as distributed on `python.org `_. The term "CPython" diff -r 661cdbd617b8 -r ea1cf086f4f4 Doc/reference/expressions.rst --- a/Doc/reference/expressions.rst Thu Apr 30 08:08:13 2015 -0700 +++ b/Doc/reference/expressions.rst Thu Apr 30 21:20:31 2015 +0100 @@ -485,6 +485,22 @@ For examples using ``yield from``, see :ref:`pep-380` in "What's New in Python." +Use As Coroutines +^^^^^^^^^^^^^^^^^ + +With the yield expression and the :meth:`~generator.send` method, generators +can be used as :term:`coroutines ` in conjunction with an event loop +or "trampoline" function. When a generator wishes to allow other generators to +run, it yields, which returns control to the event loop. The event loop uses +:meth:`~generator.send` to resume the coroutines as needed. + +Note that Python coroutines, unlike those in some other languages, cannot +choose what coroutine to transfer control to when they suspend. Instead, the +choice of who runs next must be handled by the event loop. Indeed, if +coroutines try to use :meth:`~generator.send` to transfer control directly +between each other, an exception will be raised ("ValueError: generator already +executing"). + .. _primaries:

AltStyle によって変換されたページ (->オリジナル) /