Coroutines
Similar to generators with a few differences:
generators are data producers
coroutines are data consumers
Generator blueprint:
We then commonly use it in a for
loop like this:
It is fast and does not put a lot of pressure on memory because it generates the values on the fly rather than storing them in a list.
If we use yield
in the above example, we get a coroutine.
Coroutines consume values which are sent to it.
Example: a grep
alternative in Python:
We have turned it into a coroutine. yield
does not contain any value initially, instead we supply it values externally. We supply values by using the .send()
method. Example:
The sent values are accessed by yield
.
next()
is required to start the coroutine. Just like generators
, coroutines do not start the function immediately. Instead they run it in response to the __next__()
and .send()
methods. Therefore, you have to run next()
so that the execution advances to the yield
expression.
Close a coroutine by calling the .close()
method:
Further reading
Last updated