introductory
When we think of "pythonic," comprehensions, such as list and dictionary comprehensions, are a feature of Python.
This is one way we execute loops, unlike many other languages.
Asyncio allows us to use asynchronous derivatives.
We can use asynchronous derivatives to traverse asynchronous generators and asynchronous iterators via "async for" expressions.
1. What is asynchronous derivation
Asynchronous derivatives are asynchronous versions of classic derivatives, Asyncio supports two types of asynchronous derivatives, "async for" derivatives and "await" derivatives.
Before we look at each, let's first review the classic derivation.
2. Deductive
Derivatives allow to create collections of data such as lists, dictionaries and sets in a concise way. List derivatives allow the creation of lists from for expressions in new list expressions.
... # create a list using a list comprehension result = [a*2 for a in range(100)]
Derivatives are also supported to create dictionaries and collections.
... # create a dict using a comprehension result = {a:i for a,i in zip(['a','b','c'],range(3))} # create a set using a comprehension result = {a for a in [1, 2, 3, 2, 3, 1, 5, 4]}
3. Asynchronous derivatives
Asynchronous derivatives allow lists, collections or dictionaries to be created using "async for" expressions with asynchronous iterable objects.
... # async list comprehension with an async iterator result = [a async for a in aiterable]
This creates and schedules co-programs or tasks as needed and puts the results into a list.
Recall that "async for" expressions can only be used in concurrent programs and tasks.
Also, recall that an asynchronous iterator is an iterator that produces waitable objects.
The "async for" expression allows the caller to traverse the asynchronous iterators of the waiting objects and retrieve the result from each one.
Internally, the async for loop will automatically parse or wait for each waitable scheduling coprogram as needed.
Asynchronous generators automatically implement methods for asynchronous iterators and can also be used for asynchronous derivatives.
... # async list comprehension with an async generator result = [a async for a in agenerator]
4. Await Derivative
"Wait" expressions can also be used in list, set, or dictionary comprehensions, and are called wait derivations.
As with asynchronous derivatives, it can only be used in asynchronous coprocesses or tasks.
This allows the creation of data structures, such as lists, by hanging and waiting for a series of waitable objects.
... # await list compression with a collection of awaitables results = [await a for a in awaitables]
This will create the result list by waiting for each waitable object in turn.
The current concatenation will be hung to sequentially execute the waitable objects, which is not the same as executing them concurrently with (), and may be slower.
Above is Python Asynchronous Derivative example details, more information about Python Asynchronous Derivative please pay attention to my other related articles!