The original PEP: www.python.org/dev/peps/pe…
PEP title: PEP 530 — Asynchronous Comprehensions
PEP by Yury Selivanov
Creation date: 2016-09-03
Incorporated version: 3.6
Pea flower cat @Python Cat
PEP Translation Project: github.com/chinesehuaz…
Abstract
Pep-492 and PEP-525 introduce support for native coroutines and asynchronous generators through the async/await syntax. This PEP proposes adding asynchronous versions to lists, collections, dictionary parsers, and generator expressions.
Rationale and objectives
Python has extensive support for synchronous comprehensions, allowing for the generation of lists, dictionaries, and collections using a simple and concise syntax. We propose to implement a similar syntax structure for asynchronous code.
To illustrate the improvement in readability, consider the following example:
result = []
async for i in aiter():
if i % 2:
result.append(i)
Copy the code
With the proposed asynchronous parsing syntax, the above code would be very short:
result = [i async for i in aiter() if i % 2]
Copy the code
This PEP also makes it possible to use await expressions in various parsers:
result = [await fun() for fun in funcs]
Copy the code
specification
Asynchronous parsing
We propose to allow async in list, collection, and dictionary parsing. After PEP-525 is approved, we can also create asynchronous generator expressions.
Example:
- {I async for I in agen()}
- [I async for I in agen()]
- {I: I ** 2 async for I in agen()}
- (I ** 2 async for I in agen())
Async for and if and for clauses are allowed in asynchronous parser and generator expressions:
dataset = {data for line in aiter()
async for data in line
if check(data)}
data = {data for line in aiter() async for data in line if check(data)}
Copy the code
Asynchronous parsing is only allowed in “async def” functions.
In principle, asynchronous generator expressions are allowed in any context. However, in Python 3.6, asynchronous generator expressions are only allowed in async def functions because async and await are only “soft-keyword”. Once async and await become reserved keywords in Python 3.7, this restriction will be removed.
Await in the parse
We propose to allow await expressions in both asynchronous and synchronous parsers:
result = [await fun() for fun in funcs]
result = {await fun() for fun in funcs}
result = {fun: await fun() for fun in funcs}
result = [await fun() for fun in funcs if await smth]
result = {await fun() for fun in funcs if await smth}
result = {fun: await fun() for fun in funcs if await smth}
result = [await fun() async for fun in funcs]
result = {await fun() async for fun in funcs}
result = {fun: await fun() async for fun in funcs}
result = [await fun() async for fun in funcs if await smth]
result = {await fun() async for fun in funcs if await smth}
result = {fun: await fun() async for fun in funcs if await smth}
Copy the code
This is only valid in the async DEF function body.
Syntax update
This proposal requires a syntactic change: add the optional “async” keyword to comp_for:
comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
Copy the code
The analytic AST node will have a new is_Async parameter.
Backward compatibility
This proposal is fully backward compatible.
accept
On September 6, 2016 [1], PEP-530 was accepted by Guido.
Reference material
1, mail.python.org/pipermail/p…
2, github.com/1st1/cpytho…
3, bugs.python.org/issue28008
Thank you
Thanks to Guido van Rossum, Victor Stinner, and Elvis Pranskevichuss for their feedback, code review, and discussion of this PEP.
copyright
This document is in the public domain.
Source file: github.com/python/peps…