In Python, asynchronous generator functions are coroutines, and generator functions are also coroutines.
What are the differences between the purposes of generator functions and asynchronous generator functions?
Thanks.
2. Memory Efficient: Generator Functions are memory efficient, as they save a lot of memory while using generators. A normal function will return a sequence of items, but before giving the result, it creates a sequence in memory and then gives us the result, whereas the generator function produces one output at a time.
Generator (ES6)They allow better execution control for asynchronous functionality but that does not mean they cannot be used as synchronous functionality. Note: When generator function are executed it returns a new Generator object. The pause and resume are done using yield & next .
Generator functions provide a powerful alternative: they allow you to define an iterative algorithm by writing a single function whose execution is not continuous. Generator functions are written using the function* syntax. When called, generator functions do not initially execute their code.
Async generator functions behave similarly to generator functions: the generator function returns an object that has a next() function, and calling next() executes the generator function until the next yield . The difference is that an async iterator's next() function returns a promise.
The purpose of PEP 525 -- Asynchronous Generators is pretty much similar to PEP 255 -- Simple Generators which introduced generators. It is mainly intented to make things easier to implement, only in a different domain (asynchronous one). From PEP 525:
Essentially, the goals and rationale for PEP 255, applied to the asynchronous execution case, hold true for this proposal as well.
In short, it makes writing objects that support the asynchronous iteration protocol easy. As generators did for the iterator protocol.
Instead of having to define objects that implement __aiter__
and __anext__
you create an asynchronous generator that does this seemingly by magic. This mirrors what generators did for the iterator protocol; instead of implementing __iter__
and __next__
for an object, you can just create a generator.
This is nicely stated in the rational of PEP 525 which also includes a nice example that shows the savings you make in code written when you use async generators.
In addition to code length savings, async generators also perform much better:
Performance is an additional point for this proposal: in our testing of the reference implementation, asynchronous generators are 2x faster than an equivalent implemented as an asynchronous iterator.
Just to add some terminology here because it's getting difficult to keep track of terms sometimes:
def
functions that contain one or more yield
expressions.def
+ yield
) that is wrapped by types.coroutine
. You need to wrap it in types.coroutine
if you need it to be considered a coroutine object.async def
functions that contain a one or more yield
expressions. These can also contain await
expressions.async def
without zero or more await
s and no yield
s.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With