{"id":6048,"date":"2022-04-03T16:52:22","date_gmt":"2022-04-03T08:52:22","guid":{"rendered":"https:\/\/egonlin.com\/?p=6048"},"modified":"2022-04-26T13:47:18","modified_gmt":"2022-04-26T05:47:18","slug":"%e7%ac%ac%e5%85%ab%e7%af%87%ef%bc%9a%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=6048","title":{"rendered":"\u7b2c\u4e5d\u7bc7\uff1a\u5f02\u6b65\u7f16\u7a0b"},"content":{"rendered":"<h1>\u5f02\u6b65\u7f16\u7a0b<\/h1>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps47.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps47.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u57fa\u4e8e <em>async<\/em> \u548c <em>await<\/em> \u5173\u952e\u5b57\u7684\u534f\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u8fd9\u4e5f\u662f\u76ee\u524d Python \u5f02\u6b65\u76f8\u5173\u7684\u4e3b\u6d41\u6280\u672f\u3002\u5728\u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\u5b9e\u73b0\u5f02\u6b65\u7684\u6a21\u5757\uff1aasyncio \u6a21\u5757\u3002<\/p>\n<h2>\u4e00\u3001asyncio \u6a21\u5757<\/h2>\n<p>asyncio \u6a21\u5757\u662f Python \u4e2d\u5b9e\u73b0\u5f02\u6b65\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u5728 Python3.4 \u7684\u65f6\u5019\u53d1\u5e03\uff0casync \u548c await \u5173\u952e\u5b57\u5728 Python3.5 \u4e2d\u5f15\u5165\u3002\u56e0\u6b64\uff0c\u60f3\u8981\u4f7f\u7528asyncio\u6a21\u5757\uff0c\u5efa\u8bae Python \u89e3\u91ca\u5668\u7684\u7248\u672c\u4e0d\u8981\u4f4e\u4e8e Python3.5 \u3002<\/p>\n<h2>\u4e8c\u3001\u4e8b\u4ef6\u5faa\u73af<\/h2>\n<pre><code class=\"language-python\">\u6240\u8c13\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u5f53\u4f5c\u662f\u4e00\u4e2a while \u5faa\u73af\uff0c\u8fd9\u4e2a while \u5faa\u73af\u5728\u5faa\u73af\u751f\u547d\u5468\u671f\u5185\u8fd0\u884c\u5e76\u6267\u884c\u4e00\u4e9b\u4efb\u52a1\uff0c\u5728\u7279\u5b9a\u7684\u6761\u4ef6\u4e0b\u7ed3\u675f\u5faa\u73af\u3002\n\u5728\u7f16\u5199\u7a0b\u5e8f\u7684\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u6765\u83b7\u53d6\u548c\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\uff1a<\/code><\/pre>\n<pre><code class=\"language-python\">import asyncio\n\nloop = asyncio.get_event_loop()<\/code><\/pre>\n<h2>\u4e09\u3001\u534f\u7a0b &amp; \u5f02\u6b65\u7f16\u7a0b<\/h2>\n<p>\u9996\u5148\u6211\u4eec\u6765\u770b\u4e00\u4e0b <strong><em>*\u534f\u7a0b\u51fd\u6570*<\/em><\/strong> \uff0c\u4ec0\u4e48\u662f\u534f\u7a0b\u51fd\u6570\u5462\uff1f\u76f4\u767d\u7684\u8bb2\uff0c\u5b9a\u4e49\u4e3a\u5982\u4e0b\u5f62\u5f0f\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u79f0\u4e4b\u4e3a\u534f\u7a0b\u51fd\u6570\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">async def fn():\n    pass<\/code><\/pre>\n<p>\u77e5\u9053\u4e86\u4ec0\u4e48\u662f\u534f\u7a0b\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b\u4ec0\u4e48\u662f\u534f\u7a0b\u5bf9\u8c61\uff0c\u6240\u8c13\u7684\u534f\u7a0b\u5bf9\u8c61\u5c31\u662f\u8c03\u7528\u534f\u7a0b\u51fd\u6570\u4e4b\u540e\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a <strong><em>*\u534f\u7a0b\u5bf9\u8c61*<\/em><\/strong> \uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">res = fn()<\/code><\/pre>\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a\u8c03\u7528\u534f\u7a0b\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\uff0c\u53ea\u662f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\uff01<\/p>\n<h3>1\u3001\u57fa\u672c\u5e94\u7528<\/h3>\n<p>\u5728\u7f16\u5199\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u5982\u679c\u60f3\u8981\u6267\u884c\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\uff0c\u901a\u8fc7 <strong><em>\\<\/em>\u51fd\u6570\u540d()***<em> \u8c03\u7528\u51fd\u6570\u662f\u4e0d\u53ef\u4ee5\u7684\uff0c\u9700\u8981 <\/em><\/strong>*\u4e8b\u4ef6\u5faa\u73af*<strong><em> \u548c <\/em><\/strong>*\u534f\u7a0b\u5bf9\u8c61**** \u914d\u5408\u624d\u80fd\u5b9e\u73b0\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def fn():\n    print(&#039;\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801&#039;)\n\ndef main():\n    # \u8c03\u7528\u534f\u7a0b\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\n    res = fn()\n\n    # \u6267\u884c\u534f\u7a0b\u4ee3\u7801\u7684\u65b9\u5f0f\u4e00\n    # todo\uff1a1\u3001\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\n    # loop = asyncio.get_event_loop()\n\n    # todo\uff1a2\u3001\u5c06\u534f\u7a0b\u5f53\u4f5c\u4efb\u52a1\u63d0\u4ea4\u5230\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u5217\u8868\u4e2d\uff0c\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u4e4b\u540e\u7ec8\u6b62\n    # loop.run_until_complete(res)\n\n    # \u6267\u884c\u534f\u7a0b\u4ee3\u7801\u7684\u65b9\u5f0f\u4e8c\n    # \u89e3\u6790\uff1a\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5728\u672c\u8d28\u4e0a\u548c\u7b2c\u4e00\u79cd\u65b9\u5f0f\u662f\u76f8\u540c\u7684\uff0c\u5176\u5185\u90e8\u5148\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\uff0c\u7136\u540e\u6267\u884c run_until_complete\n    # \u4f46\u662f\u8981\u6ce8\u610f\uff1a\u8be5\u65b9\u5f0f\u53ea\u652f\u6301 Python3.7+ \u7684\u89e3\u91ca\u5668\uff0c\u56e0\u4e3a\u8be5\u65b9\u5f0f\u5728 Python3.7 \u52a0\u5165\u7684\u3002\n    asyncio.run(res)\n\nif __name__ == &#039;__main__&#039;:\n    main()<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\uff1a\u5c06 <strong><em>\\<\/em>\u534f\u7a0b\u51fd\u6570***<em> \u5f53\u505a\u4efb\u52a1\u6dfb\u52a0\u5230 <\/em><\/strong>*\u4e8b\u4ef6\u5faa\u73af*<strong><em> \u7684\u4efb\u52a1\u5217\u8868\uff0c\u7136\u540e\u4e8b\u4ef6\u5faa\u73af\u68c0\u6d4b\u5217\u8868\u4e2d\u7684<\/em><\/strong>*\u534f\u7a0b\u51fd\u6570**** \u662f\u5426\u5df2\u51c6\u5907\u5c31\u7eea\uff08\u9ed8\u8ba4\u53ef\u7406\u89e3\u4e3a\u5c31\u7eea\u72b6\u6001\uff09\uff0c\u5982\u679c\u51c6\u5907\u5c31\u7eea\u5219\u6267\u884c\u5176\u5185\u90e8\u4ee3\u7801\u3002<\/p>\n<h3>2\u3001await<\/h3>\n<pre><code class=\"language-bash\">await \u662f\u4e00\u4e2a \u53ea\u80fd \u5728\u534f\u7a0b\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u5f53\u534f\u7a0b\u51fd\u6570\u9047\u5230IO\u64cd\u4f5c\u7684\u65f6\u5019\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff08\u4efb\u52a1\uff09\uff0c\u5f53\u524d\u534f\u7a0b\u6302\u8d77\u8fc7\u7a0b\u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u7684\u534f\u7a0b\uff08\u4efb\u52a1\uff09\uff0c\u5f53\u524d\u534f\u7a0bIO\u5904\u7406\u5b8c\u6210\u65f6\uff0c\u53ef\u4ee5\u518d\u6b21\u5207\u6362\u56de\u6765\u6267\u884c await \u4e4b\u540e\u7684\u4ee3\u7801\uff0c\u600e\u4e48\u7406\u89e3\u5462\uff1f\u8bf7\u770b\u5982\u4e0b\u4ee3\u7801\uff1a<\/code><\/pre>\n<p><strong><em>*\u5b9e\u4f8b1*<\/em><\/strong><\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def fn():\n    print(&#039;\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801&#039;)\n\n    # \u9047\u5230IO\u64cd\u4f5c\u4e4b\u540e\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff08\u4efb\u52a1\uff09\uff0c\u7b49IO\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002\n    # \u5f53\u524d\u534f\u7a0b\u6302\u8d77\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u534f\u7a0b\uff08\u4efb\u52a1\uff09\n    response = await asyncio.sleep(2)  # \u6a21\u62df\u9047\u5230\u4e86IO\u64cd\u4f5c\n\n    print(f&#039;IO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a{response}&#039;)\n\ndef main():\n    # \u8c03\u7528\u534f\u7a0b\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\n    res = fn()\n\n    # \u6267\u884c\u534f\u7a0b\u51fd\u6570\n    asyncio.run(res)\n\nif __name__ == &#039;__main__&#039;:\n    main()\n\n&#039;&#039;&#039;\n\u8fd0\u884c\u7ed3\u679c\uff1a\n\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\nIO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1aNone\n&#039;&#039;&#039;<\/code><\/pre>\n<p><strong><em>*\u5b9e\u4f8b2*<\/em><\/strong><\/p>\n<pre><code class=\"language-bash\">import asyncio\n\nasync def other_tasks():\n    print(&#039;start&#039;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u9047\u5230\u4e86IO\u64cd\u4f5c\n    print(&#039;end&#039;)\n    return &#039;\u8fd4\u56de\u503c&#039;\n\nasync def fn():\n    print(&#039;\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801&#039;)\n\n    # \u9047\u5230IO\u64cd\u4f5c\u4e4b\u540e\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff08\u4efb\u52a1\uff09\uff0c\u7b49IO\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002\n    # \u5f53\u524d\u534f\u7a0b\u6302\u8d77\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u534f\u7a0b\uff08\u4efb\u52a1\uff09\n    response = await other_tasks()  # \u6a21\u62df\u6267\u884c\u5176\u4ed6\u534f\u7a0b\u4efb\u52a1\n\n    print(f&#039;IO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a{response}&#039;)\n\ndef main():\n    # \u8c03\u7528\u534f\u7a0b\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\n    res = fn()\n\n    # \u6267\u884c\u534f\u7a0b\u51fd\u6570\n    asyncio.run(res)\n\nif __name__ == &#039;__main__&#039;:\n    main()\n\n&#039;&#039;&#039;\n\u8fd0\u884c\u7ed3\u679c\uff1a\n\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\nstart\nend\nIO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a\u8fd4\u56de\u503c\n&#039;&#039;&#039;<\/code><\/pre>\n<p><strong><em>*\u5b9e\u4f8b3*<\/em><\/strong><\/p>\n<pre><code class=\"language-bash\">import asyncio\n\nasync def other_tasks():\n    print(&#039;start&#039;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u9047\u5230\u4e86IO\u64cd\u4f5c\n    print(&#039;end&#039;)\n    return &#039;\u8fd4\u56de\u503c&#039;\n\nasync def fn():\n    print(&#039;\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801&#039;)\n\n    # \u9047\u5230IO\u64cd\u4f5c\u4e4b\u540e\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff08\u4efb\u52a1\uff09\uff0c\u7b49IO\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002\n    # \u5f53\u524d\u534f\u7a0b\u6302\u8d77\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u534f\u7a0b\uff08\u4efb\u52a1\uff09\n    respnse1 = await other_tasks()\n    print(f&#039;IO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a{respnse1}&#039;)\n\n    respnse2 = await other_tasks()\n    print(f&#039;IO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a{respnse2}&#039;)\n\ndef main():\n    # \u8c03\u7528\u534f\u7a0b\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\n    cor_obj = fn()\n\n    # \u6267\u884c\u534f\u7a0b\u51fd\u6570\n    asyncio.run(cor_obj)\n\nif __name__ == &#039;__main__&#039;:\n    main()\n\n&#039;&#039;&#039;\n\u8fd0\u884c\u7ed3\u679c\uff1a\n\u534f\u7a0b\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\nstart\nend\nIO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a\u8fd4\u56de\u503c\nstart\nend\nIO\u8bf7\u6c42\u7ed3\u675f\uff0c\u7ed3\u679c\u4e3a\uff1a\u8fd4\u56de\u503c\n&#039;&#039;&#039;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7684\u6240\u6709\u5b9e\u4f8b\u90fd\u53ea\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u4efb\u52a1\uff0c\u5373\uff1a\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u5217\u8868\u4e2d\u53ea\u6709\u4e00\u4e2a\u4efb\u52a1\uff0c\u6240\u4ee5\u5728IO\u7b49\u5f85\u65f6\u65e0\u6cd5\u6f14\u793a\u5207\u6362\u5230\u5176\u4ed6\u4efb\u52a1\u6548\u679c\u3002<\/p>\n<p>\u5728\u7a0b\u5e8f\u4e2d\u60f3\u8981\u521b\u5efa\u591a\u4e2a\u4efb\u52a1\u5bf9\u8c61\uff0c\u9700\u8981\u4f7f\u7528Task\u5bf9\u8c61\u6765\u5b9e\u73b0\u3002<\/p>\n<h3>3\u3001Task \u5bf9\u8c61<\/h3>\n<pre><code class=\"language-bash\">Tasks \u7528\u4e8e\u5e76\u53d1\u8c03\u5ea6\u534f\u7a0b\uff0c\u901a\u8fc7 asyncio.create_task(\u534f\u7a0b\u5bf9\u8c61) \u7684\u65b9\u5f0f\u521b\u5efa Task \u5bf9\u8c61\uff0c\u8fd9\u6837\u53ef\u4ee5\u8ba9\u534f\u7a0b\u52a0\u5165\u4e8b\u4ef6\u5faa\u73af\u4e2d\u7b49\u5f85\u88ab\u8c03\u5ea6\u6267\u884c\u3002\u9664\u4e86\u4f7f\u7528 asyncio.create_task() \u51fd\u6570\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u7528\u4f4e\u5c42\u7ea7\u7684loop.create_task() \u6216 ensure_future() \u51fd\u6570\u3002\u5e76\u4e14\u4e0d\u5efa\u8bae\u624b\u52a8\u5b9e\u4f8b\u5316 Task \u5bf9\u8c61\u3002\n\u672c\u8d28\u4e0a\u662f\u5c06\u534f\u7a0b\u5bf9\u8c61\u5c01\u88c5\u6210 Task \u5bf9\u8c61\uff0c\u5e76\u5c06\u534f\u7a0b\u7acb\u5373\u52a0\u5165\u4e8b\u4ef6\u5faa\u73af\uff0c\u540c\u65f6\u8ffd\u8e2a\u534f\u7a0b\u7684\u72b6\u6001\u3002\n\u6ce8\u610f\u4e8b\u9879\uff1aasyncio.create_task() \u51fd\u6570\u5728 Python3.7 \u4e2d\u88ab\u52a0\u5165\u3002\u5728 Python3.7 \u4e4b\u524d\uff0c\u53ef\u4ee5\u6539\u7528\u4f4e\u5c42\u7ea7\u7684 asyncio.ensure_future() \u51fd\u6570\u3002<\/code><\/pre>\n<p><strong><em>*\u5b9e\u4f8b1*<\/em><\/strong><\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def other_tasks():\n    print(&#039;start&#039;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u9047\u5230\u4e86IO\u64cd\u4f5c\n    print(&#039;end&#039;)\n    return &#039;\u8fd4\u56de\u503c&#039;\n\nasync def fn():\n    print(&#039;fn\u5f00\u59cb&#039;)\n\n    # \u521b\u5efa\u534f\u7a0b\uff0c\u5c06\u534f\u7a0b\u5c01\u88c5\u5230\u4e00\u4e2aTask\u5bf9\u8c61\u4e2d\u5e76\u7acb\u5373\u6dfb\u52a0\u5230\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u5217\u8868\u4e2d\uff0c\u7b49\u5f85\u4e8b\u4ef6\u5faa\u73af\u53bb\u6267\u884c\uff08\u9ed8\u8ba4\u662f\u5c31\u7eea\u72b6\u6001\uff09\u3002\n    task1 = asyncio.create_task(other_tasks())\n\n    # \u521b\u5efa\u534f\u7a0b\uff0c\u5c06\u534f\u7a0b\u5c01\u88c5\u5230\u4e00\u4e2aTask\u5bf9\u8c61\u4e2d\u5e76\u7acb\u5373\u6dfb\u52a0\u5230\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u5217\u8868\u4e2d\uff0c\u7b49\u5f85\u4e8b\u4ef6\u5faa\u73af\u53bb\u6267\u884c\uff08\u9ed8\u8ba4\u662f\u5c31\u7eea\u72b6\u6001\uff09\u3002\n    task2 = asyncio.create_task(other_tasks())\n\n    print(&#039;fn\u7ed3\u675f&#039;)\n\n    # \u5f53\u6267\u884c\u67d0\u534f\u7a0b\u9047\u5230IO\u64cd\u4f5c\u65f6\uff0c\u4f1a\u81ea\u52a8\u5316\u5207\u6362\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002\n    # \u6b64\u5904\u7684await\u662f\u7b49\u5f85\u76f8\u5bf9\u5e94\u7684\u534f\u7a0b\u5168\u90fd\u6267\u884c\u5b8c\u6bd5\u5e76\u83b7\u53d6\u7ed3\u679c\n    response1 = await task1\n    response2 = await task2\n    print(response1, response2)\n\ndef main():\n    asyncio.run(fn())\n\nif __name__ == &#039;__main__&#039;:\n    main()\n\n&#039;&#039;&#039;\n\u8fd0\u884c\u7ed3\u679c\uff1a\nfn\u5f00\u59cb\nfn\u7ed3\u675f\nstart\nstart\nend\nend\n\u8fd4\u56de\u503c \u8fd4\u56de\u503c\n&#039;&#039;&#039;<\/code><\/pre>\n<p><strong><em>*\u5b9e\u4f8b2*<\/em><\/strong><\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def other_tasks():\n    print(&#039;start&#039;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u9047\u5230\u4e86IO\u64cd\u4f5c\n    print(&#039;end&#039;)\n    return &#039;\u8fd4\u56de\u503c&#039;\n\nasync def fn():\n    print(&#039;fn\u5f00\u59cb&#039;)\n\n    # \u521b\u5efa\u534f\u7a0b\uff0c\u5c06\u534f\u7a0b\u5c01\u88c5\u5230\u4e00\u4e2aTask\u5bf9\u8c61\u4e2d\u5e76\u7acb\u5373\u6dfb\u52a0\u5230\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u5217\u8868\u4e2d\uff0c\u7b49\u5f85\u4e8b\u4ef6\u5faa\u73af\u53bb\u6267\u884c\uff08\u9ed8\u8ba4\u662f\u5c31\u7eea\u72b6\u6001\uff09\u3002\n    task_lis = [\n        asyncio.create_task(other_tasks()),\n        asyncio.create_task(other_tasks()),\n    ]\n\n    print(&#039;fn\u7ed3\u675f&#039;)\n    # \u5f53\u6267\u884c\u67d0\u534f\u7a0b\u9047\u5230IO\u64cd\u4f5c\u65f6\uff0c\u4f1a\u81ea\u52a8\u5316\u5207\u6362\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002\n    # \u6b64\u5904\u7684await\u662f\u7b49\u5f85\u6240\u6709\u534f\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u5e76\u5c06\u6240\u6709\u534f\u7a0b\u7684\u8fd4\u56de\u503c\u4fdd\u5b58\u5230done\n    # \u5982\u679c\u8bbe\u7f6e\u4e86timeout\u503c\uff0c\u5219\u610f\u5473\u7740\u6b64\u5904\u6700\u591a\u7b49\u5f85\u7684\u79d2\uff0c\u5b8c\u6210\u7684\u534f\u7a0b\u8fd4\u56de\u503c\u5199\u5165\u5230done\u4e2d\uff0c\u672a\u5b8c\u6210\u5219\u5199\u5230pending\u4e2d\u3002\n    done, pending = await asyncio.wait(task_lis, timeout=None)\n\n    print(done)\n    print(pending)\n\ndef main():\n    asyncio.run(fn())\n\nif __name__ == &#039;__main__&#039;:\n    main()\n\n&#039;&#039;&#039;\n\u8fd0\u884c\u7ed3\u679c\uff1a\nfn\u5f00\u59cb\nfn\u7ed3\u675f\nstart\nstart\nend\nend\n{&lt;Task finished name=&#039;Task-3&#039; coro=&lt;other_tasks() done, defined at \/Users\/liumingyang\/PycharmProjects\/async_test\/test.py:4&gt; result=&#039;\u8fd4\u56de\u503c&#039;&gt;, &lt;Task finished name=&#039;Task-2&#039; coro=&lt;other_tasks() done, defined at \/Users\/liumingyang\/PycharmProjects\/async_test\/test.py:4&gt; result=&#039;\u8fd4\u56de\u503c&#039;&gt;}\nset()\n&#039;&#039;&#039;<\/code><\/pre>\n<h3>4\u3001\u5f02\u6b65\u8fed\u4ee3\u5668<\/h3>\n<p><\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"6048\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"6048\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"2b783ef66c\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"7755f0f36d\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65\u7f16\u7a0b \u57fa\u4e8e async \u548c await \u5173\u952e\u5b57\u7684\u534f\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u8fd9\u4e5f\u662f\u76ee\u524d Python \u5f02\u6b65\u76f8\u5173\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6049,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,281,370],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6048"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6048"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6048\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/6049"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}