Wait, it's all generators?
Yes! Generators in Python are quite fancy. While they are generally seen as an easier way to create iterators, they also have the ability to consume values. Generators are the backbone for asyncio
in Python as the keywords async
and await
are merely syntatical sugar for various forms of yield
statements.
How are you using it?
We use generators via manually running next()
and .send()
functions to resume the generator state. Essentially, the business logic needs to step out and receive and send information to IO to compute. yield
statements essentially allow us to "pause" the internal context while a different function implemented by a SubgroundsBase
subclass to actually compute.
But why?
The approach, known as sans-io, we've chosen here has allowed us to reduce a large amount of code duplication when producing an async
version of our API. It also disentangles the IO aspect of the library with the business logic allowing anyone to write in their own IO with very little work.
Where can I learn more?