Generators, coroutines, and nanoservices

Understanding and using one of Python's lesser-known features

Reuven Lerner

ASYNC / Concurreny Python Skills Python general

See in schedule

Generator functions have been a part of Python for many years already, and are a well known technique for creating iterators. But generators have a few lesser-known aspects, including their “send” method and the “yield from” syntax. Many Python developers shy away from using them, unsure of what they would do, or how they would be useful — seeing coroutines as a solution looking for a problem.

In this talk, I’ll tell you why coroutines can be useful, and how thinking about them as in-process “nanoservices” puts us in the right frame of mind to determine when they would and wouldn’t be appropriate. Among the topics we’ll explore are:

Generator function basics
next vs. send
How to build a coroutine
Thinking of coroutines as nanoservices
Sub-coroutines
The need for yield from
Using yield from
Ideas for general usage

Type: Talk (30 mins); Python level: Advanced; Domain level: Beginner


Reuven Lerner

Lerner Consulting

Reuven teaches Python, data science, and Git to companies around the world. He also offers many online courses, including "Weekly Python Exercise," a community-based way to improve your Python fluency over time. Reuven writes two free, weekly newsletters — "Better developers" (about Python development, with more than 20,000 subscribers) and "Trainer weekly" (for technical trainers who want to improve their skills). Reuven is the author of "Python Workout" from Manning, and is a co-host of the "Business of Freelancing" podcast. More information about Reuven, his courses, and writing is at https://lerner.co.il/ .