1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Python 列表解析式竟然支持異步?

        共 3744字,需瀏覽 8分鐘

         ·

        2021-08-24 17:39


        花下貓語:近期一直在忙于其它事,導致沒有創(chuàng)作新文章,甚至日常發(fā)文也變得稀少了。我想到一個稍微改變這種不良局面的辦法,那就是找一些文章/文檔來翻譯,因為可以利用上下班通勤及午休這種碎片時間。今天發(fā)的還是一篇PEP譯文,可能價值不是很大吧,但也算是擠著時間做了一點實際的事。如果你看到不錯的英文技術文章,或者很感興趣的PEP文檔,歡迎給我留言,我酌情翻譯。

        PEP原文:https://www.python.org/dev/peps/pep-0530
        PEP標題:PEP 530 -- Asynchronous Comprehensions
        PEP作者:Yury Selivanov
        創(chuàng)建日期:2016-09-03
        合入版本:3.6
        譯者:豌豆花下貓@Python貓

        PEP翻譯計劃:https://github.com/chinesehuazhou/peps-cn

        摘要

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

        基本原理和目標

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

        為了說明可讀性的改善,請考慮下面的例子:

        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]

        規(guī)范

        異步的解析式

        我們提議允許在列表、集合與字典解析式中使用 async。待 PEP-525 被批準之后,我們還可以創(chuàng)建異步的生成器表達式。

        例子:

        • 集合解析式:{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 以及 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ù)中使用。

        原則上,異步生成器表達式允許用在任何上下文中。然而,在 Python 3.6 中,由于 async 和 await 只是“軟關鍵字”(soft-keyword),異步生成器表達式只允許在 async def 函數(shù)中使用。一旦 async 和 await 在 Python 3.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 函數(shù)體中有效。

        語法的更新

        本提議需要在語法層面做一個修改:在 comp_for 中添加可選的“async”關鍵字:

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

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

        向后兼容性

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

        接受

        在 2016 年 9 月 6 日[1],PEP-530 被 Guido 接受。

        參考材料

        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

        致謝

        感謝 Guido van Rossum、Victor Stinner 和 Elvis pranskevichuss 對于這個 pep 的反饋、代碼檢視和討論。

        版權

        本文檔已進入公共領域。

        源文件:https://github.com/python/peps/blob/master/pep-0530.txt

        瀏覽 36
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            黄色无码小视频 | 91在线无码精品秘 传媒 | 蜜桃在线码无精品秘 入口九色 | 免费成人18禁 | 五月婷香蕉久色在线看 | 国产成人福利在线观看 | 色A网 | 女人做爰全过程免费观看美女 | 亚洲视频免费在线看 | 91久久爽无码人妻AⅤ精品蜜桃 |