Part 2 · Section 12
Generator Based Coroutines
So far generators have been a way to produce values lazily. But a generator is really a function that can pause and resume while keeping its local state, and that idea goes much further than iteration. If the caller can also hand data back in each time it resumes the generator, the generator becomes a coroutine: a routine that cooperates with others by voluntarily giving up control.
This is the machinery that Python’s async/await grew out of. Modern code writes native coroutines with async def, but the mechanics (suspension, resumption, sending values in, throwing exceptions in, delegating with yield from) are exactly what you’ll learn here, using plain generators you can poke at line by line.
In this section you’ll learn:
- what cooperative multitasking means, and how
yieldlets a function hand control back to its caller - the four states of a generator (created, running, suspended, closed) and how to inspect them
- sending data into a generator with
send(), and why a coroutine must be primed first - closing generators with
close()and handlingGeneratorExitcorrectly - throwing exceptions into a generator with
throw(), and using exceptions as control signals - writing a decorator that primes coroutines automatically
- how
yield fromcreates a two-way channel between a caller and a subgenerator, forwardingnext,send,closeandthrow, and how it captures a subgenerator’s return value - building pull pipelines with generators, push pipelines with coroutines, and broadcasting data to several branches
Lectures
- 01Coroutines✓
- 02Generator States✓
- 03Generator States✓
- 04Sending Data To Generators✓
- 05Closing Generators✓
- 06Sending Exceptions to Generators✓
- 07Using Decorators to Prime Coroutines✓
- 08Yield From - Two-Way Communications✓
- 09Yield From - Sending Data✓
- 10Yield From - Closing and Return✓
- 11Yield From - Throwing Exceptions✓
- 12Pipelines - Pulling Data✓
- 13Pipelines - Pushing Data✓
- 14Pipelines - Broadcasting✓