Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between the purposes of generator functions and asynchronous generator functions

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.

like image 666
Tim Avatar asked Sep 13 '17 17:09

Tim


People also ask

What is the difference between generator and function?

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.

Are generator functions asynchronous?

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 .

What are generators functions?

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.

What are async generators?

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.


1 Answers

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:

  • Generators: def functions that contain one or more yield expressions.
  • Generator-based coroutine: A generator (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.
  • Asynchronous Generator: async def functions that contain a one or more yield expressions. These can also contain await expressions.
  • Coroutine: async def without zero or more awaits and no yields.
like image 194
Dimitris Fasarakis Hilliard Avatar answered Sep 29 '22 19:09

Dimitris Fasarakis Hilliard