Generator Based Coroutines · Lecture 08 · 12 min
Yield From - Two-Way Communications
yield from connects the caller directly to a subgenerator through the delegating generator, and the delegator resumes only when the subgenerator finishes.
Delegation, revisited
In the generators section we used yield from as a shortcut for “yield everything this iterable produces”:
def squares(n):
for i in range(n):
yield i ** 2
def delegator_loop(n):
for value in squares(n):
yield value
def delegator(n):
yield from squares(n)
print(list(delegator_loop(5)))
print(list(delegator(5)))For plain iteration the two are equivalent. But yield from does much more than the loop version, and that’s what makes it central to coroutines.
Terminology. In yield from subgen:
- the generator containing the
yield fromis the delegator; - the generator it delegates to is the subgenerator;
- the code calling
next()(orsend(), …) on the delegator is the caller.
A two-way channel
When the caller calls next() on the delegator, the delegator doesn’t do any work itself. It passes the request straight to the subgenerator. The subgenerator yields a value, and that value travels back through the delegator to the caller. While the yield from is active, the delegator is just a transparent pipe:
yield from runs, the delegator forwards every request to the subgenerator and every yielded value back to the caller.The next lessons show that send(), close() and throw() travel through this channel too. First, let’s watch the states on both ends.
Watching delegator and subgenerator
from inspect import getgeneratorstate, getgeneratorlocals
def song():
yield "I'm a lumberjack and I'm OK"
yield 'I sleep all night and I work all day'
def play_song():
count = 0
s = song()
yield from s
yield 'song finished'
print('player is exiting...')
player = play_song()
print(getgeneratorstate(player), getgeneratorlocals(player))The delegator hasn’t started, so it has no locals yet (not even s). Start it:
print(next(player))
print(getgeneratorstate(player), getgeneratorlocals(player))
s = getgeneratorlocals(player)['s']
print('subgen:', getgeneratorstate(s))One next() on the delegator started both generators: play_song ran to its yield from, which immediately advanced song to its first yield. The value came straight back to us. Both are now suspended: the subgenerator at its yield, the delegator at its yield from.
print(next(player))
print('delegator:', getgeneratorstate(player), '| subgen:', getgeneratorstate(s))Still both suspended. The next request is the interesting one: the subgenerator has nothing left to yield, so it returns. The yield from sees that, finishes, and the delegator carries on with its own code:
print(next(player))
print('delegator:', getgeneratorstate(player), '| subgen:', getgeneratorstate(s))yield from absorbed the subgenerator’s StopIteration (the caller never saw it), and the delegator ran on to its next yield. One more next() finishes the delegator itself, and this time the StopIteration does reach us:
try:
next(player)
except StopIteration:
print('StopIteration')
print('delegator:', getgeneratorstate(player), '| subgen:', getgeneratorstate(s))Delegating repeatedly
Since the delegator resumes normally after each yield from, it can delegate again and again, to the same generator function or to different ones:
def player():
count = 1
while True:
print('Run count:', count)
yield from song()
count += 1
p = player()
print(next(p), '/', next(p))
print(next(p), '/', next(p))
print(next(p), '/', next(p))Each pass through the loop creates a new song() subgenerator and delegates to it until it’s exhausted, then the delegator’s own code (count += 1) runs before the next delegation.
for v in subgen: yield v only forwards next(). Values sent with send() would land in the delegator’s yield and be thrown away, a throw() would hit the delegator instead of the subgenerator, and the subgenerator’s return value would be lost. yield from forwards all of these correctly, which is why it was added (PEP 380, Python 3.3) and why it became the basis for coroutine delegation.
Key takeaways
- In
yield from subgen, the generator doing the delegating is the delegator,subgenis the subgenerator, and whoever drives the delegator is the caller. yield fromcreates a two-way channel: the caller’s requests go to the subgenerator, and its yielded values come back to the caller.- The first
next()on the delegator also primes the subgenerator. - When the subgenerator finishes,
yield fromswallows itsStopIterationand the delegator resumes with the code after it. - Unlike a
forloop that re-yields values,yield fromalso forwardssend(),throw()andclose(), and captures the subgenerator’s return value.