PEP 530 -- 异步推导式

PEP 530 -- 异步推导式

摘要

PEP 492和PEP 525使用async/await语法引入了协程。PEP 530建议添加list,set,dict推导式和生成器推导式的异步版本。

理论和目标

对如下代码做了改进。

result = []
async for i in aiter():
    if i % 2:
        result.append(i)

可以写成异步推导式的方式简化上面代码:

result = [i async for i in aiter() if i % 2]

同样的该pep也支持上述各种表达式里面使用await语句,像下面这样

result = [await fun() for fun in funcs]

详细内容

同样的我们可以创建set,list,dict的异步推导式。

异步集合推导式: {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来生成推导式的时候,使用条件语句if,就像下面这样

dataset = {data for line in aiter()
                async for data in line
                if check(data)}

需要注意的是到python3.6的时候async和await必须配合async def函数使用,不能写在异步函数之外,但是在python3.7的时候这个限制将被解除。

推导式里使用await

异步推导式

await可以用到同步表达式和异步表达式中,像下面的代码这样

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}

以上语句只能在async def函数体中使用

语句更新

该提议需要对语法级别进行一次更改:向comp_for添加可选的“async”关键字:

comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]

python 推导式AST部分将加入is_async参数。这个实际上就是说加入字典异步推导式的实现,像同步那样。

是否支持向后兼容

支持向后兼容

提供人

PEP 530 由 Guido, 2016年9月6日提出.

参考资料

[1] https://mail.python.org/pipermail/python-ideas/2016-September/042141.html
[2] https://github.com/1st1/cpython/tree/asyncomp
[3] http://bugs.python.org/issue28008

版权声明

本文章翻译整理自,pep530
Source: https://github.com/python/peps/blob/master/pep-0530.txt

原文地址:https://www.cnblogs.com/c-x-a/p/10306870.html