日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python支持異步的列表解析式_python

作者:Python編程學(xué)習(xí)圈 ? 更新時(shí)間: 2022-09-08 編程語(yǔ)言

摘要

PEP-492 和 PEP-525 通過(guò) async/await 語(yǔ)法,引入了對(duì)原生協(xié)程和異步生成器的支持。本 pep 提議給列表、集合、字典解析式和生成器表達(dá)式添加異步的版本。

基本原理和目標(biāo)

Python 廣泛地支持同步的推導(dǎo)式,允許使用簡(jiǎn)單而簡(jiǎn)潔的語(yǔ)法生成列表、字典和集合。我們提議為異步代碼實(shí)現(xiàn)類似的語(yǔ)法結(jié)構(gòu)。

為了說(shuō)明可讀性的改善,請(qǐng)考慮下面的例子:

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

有了提議的異步解析式語(yǔ)法,上面的代碼會(huì)變得非常簡(jiǎn)短:

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

本 PEP 也使得在各種解析式中使用 await 表達(dá)式成為可能:

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

規(guī)范

異步的解析式

我們提議允許在列表、集合與字典解析式中使用 async。待 PEP-525 被批準(zhǔn)之后,我們還可以

創(chuàng)建異步的生成器表達(dá)式。

例子:

集合解析式:{i async for i in agen()}

列表解析式:[i async for i in agen()]

字典解析式:{i: i ** 2 async for i in agen()}

生成器表達(dá)式:(i ** 2 async for i in agen())

允許在異步解析式和生成器表達(dá)式中使用 async for 與 if 以及 for 子句:

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)}

異步解析式只允許在“async def”函數(shù)中使用。

原則上,異步生成器表達(dá)式允許用在任何上下文中。然而,在 Python 3.6 中,由于 async 和 await 只是“軟關(guān)鍵字”(soft-keyword),異步生成器表達(dá)式只允許在 async def 函數(shù)中使用。一旦 async 和 await 在 Python 3.7 中成為保留關(guān)鍵字,這個(gè)限制將被移除。

解析式中的 await

我們提議允許在異步和同步解析式中使用 await 表達(dá)式:

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 函數(shù)體中有效。

語(yǔ)法的更新

本提議需要在語(yǔ)法層面做一個(gè)修改:在 comp_for 中添加可選的“async”關(guān)鍵字:

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

解析式的 AST 節(jié)點(diǎn)將有一個(gè)新的 is_async 參數(shù)。

向后兼容性?本提案是完全向后兼容的。

原文鏈接:https://segmentfault.com/a/1190000042114527

欄目分類
最近更新