{"id":9250,"date":"2024-04-21T23:47:27","date_gmt":"2024-04-21T15:47:27","guid":{"rendered":"\/?p=9250"},"modified":"2024-04-23T01:08:06","modified_gmt":"2024-04-22T17:08:06","slug":"python%e5%8d%8f%e7%a8%8b-asyncio","status":"publish","type":"post","link":"\/?p=9250","title":{"rendered":"Python\u534f\u7a0b asyncio"},"content":{"rendered":"<p>\u5728\u4e86\u89e3\u4e86 Python \u5e76\u53d1\u7f16\u7a0b\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u4e4b\u540e,\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0b\u57fa\u4e8e asyncio \u7684\u5f02\u6b65IO\u7f16\u7a0b--\u534f\u7a0b<\/p>\n<h1>\u534f\u7a0b\u7b80\u4ecb<\/h1>\n<p>\u534f\u7a0b(Coroutine)\u53c8\u79f0\u5fae\u7ebf\u7a0b\u3001\u7ea4\u7a0b\uff0c\u534f\u7a0b\u4e0d\u662f\u8fdb\u7a0b\u6216\u7ebf\u7a0b\uff0c\u5176\u6267\u884c\u8fc7\u7a0b\u7c7b\u4f3c\u4e8e Python \u51fd\u6570\u8c03\u7528\uff0cPython \u7684 asyncio \u6a21\u5757\u5b9e\u73b0\u7684\u5f02\u6b65IO\u7f16\u7a0b\u6846\u67b6\u4e2d\uff0c\u534f\u7a0b\u662f\u5bf9\u4f7f\u7528 async \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u5f02\u6b65\u51fd\u6570\u7684\u8c03\u7528;<\/p>\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u5305\u542b\u591a\u4e2a\u7ebf\u7a0b,\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u4eba\u4f53\u7ec4\u7ec7\u6709\u591a\u79cd\u7ec6\u80de\u5728\u5de5\u4f5c\uff0c\u540c\u6837\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u534f\u7a0b\u3002\u591a\u4e2a\u7ebf\u7a0b\u76f8\u5bf9\u72ec\u7acb\uff0c\u7ebf\u7a0b\u7684\u5207\u6362\u53d7\u7cfb\u7edf\u63a7\u5236\u3002\u540c\u6837\uff0c\u591a\u4e2a\u534f\u7a0b\u4e5f\u76f8\u5bf9\u72ec\u7acb\uff0c\u4f46\u662f\u5176\u5207\u6362\u7531\u7a0b\u5e8f\u81ea\u5df1\u63a7\u5236\u3002<\/p>\n<h1>\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50<\/h1>\n<p>\u6211\u4eec\u6765\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e86\u89e3\u534f\u7a0b\uff0c\u9996\u5148\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801:<\/p>\n<pre><code class=\"language-python\">import time\ndef display(num):\n    time.sleep(1)\n    print(num)\nfor num in range(10):\n    display(num)<\/code><\/pre>\n<p>\u5f88\u5bb9\u6613\u770b\u5f97\u61c2\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa0\u52309\u7684\u6570\u5b57\uff0c\u6bcf\u96941\u79d2\u4e2d\u8f93\u51fa\u4e00\u4e2a\u6570\u5b57\uff0c\u56e0\u6b64\u6574\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u9700\u8981\u5927\u7ea610\u79d2 \u65f6\u95f4\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b(\u5e76\u53d1)\uff0c\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2a\u6267\u884c\u5355\u5143(\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728 \u6267\u884c)\uff0c\u800c time.sleep(1) \u7684\u4f11\u7720\u64cd\u4f5c\u4f1a\u8ba9\u6574\u4e2a\u7ebf\u7a0b\u505c\u6ede1\u79d2\u949f\uff0c<\/p>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u4ee3\u7801\u6765\u8bf4\uff0c\u5728\u8fd9\u6bb5\u65f6\u95f4\u91cc\u9762 CPU\u662f\u95f2\u7f6e\u7684\u6ca1\u6709\u505a\u4ec0\u4e48\u4e8b\u60c5\u3002<\/p>\n<p>\u6211\u4eec\u518d\u6765\u770b\u770b\u4f7f\u7528\u534f\u7a0b\u4f1a\u53d1\u751f\u4ec0\u4e48:<\/p>\n<pre><code class=\"language-python\">import asyncio\nasync def display(num): # \u5728\u51fd\u6570\u524d\u4f7f\u7528async\u5173\u952e\u5b57,\u53d8\u6210\u5f02\u6b65\u51fd\u6570 \n    await asyncio.sleep(1)\n    print(num)\n<\/code><\/pre>\n<p>\u5f02\u6b65\u51fd\u6570\u4e0d\u540c\u4e8e\u666e\u901a\u51fd\u6570\uff0c\u8c03\u7528\u666e\u901a\u51fd\u6570\u4f1a\u5f97\u5230\u8fd4\u56de\u503c\uff0c\u800c\u8c03\u7528\u5f02\u6b65\u51fd\u6570\u4f1a\u5f97\u5230\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002\u6211\u4eec\u9700\u8981\u5c06\u534f\u7a0b\u5bf9\u8c61\u653e\u5230\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u4e2d\u624d\u80fd\u8fbe\u5230\u4e0e\u5176\u4ed6\u534f\u7a0b\u5bf9\u8c61\u534f\u4f5c\u7684\u6548\u679c\uff0c\u56e0\u4e3a\u4e8b\u4ef6\u5faa\u73af\u4f1a\u8d1f\u8d23\u5904\u7406\u5b50\u7a0b \u5e8f\u5207\u6362\u7684\u64cd\u4f5c\u3002<br \/>\n\u7b80\u5355\u7684\u8bf4\u5c31\u662f\u8ba9\u963b\u585e\u7684\u5b50\u7a0b\u5e8f\u8ba9\u51faCPU\u7ed9\u53ef\u4ee5\u6267\u884c\u7684\u5b50\u7a0b\u5e8f\u3002<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/1024536-20220805174624988-512708079.png\" alt=\"image\" \/><\/p>\n<h1>\u57fa\u672c\u6982\u5ff5<\/h1>\n<p>\u5f02\u6b65IO\u662f\u6307\u7a0b\u5e8f\u53d1\u8d77\u4e00\u4e2aIO\u64cd\u4f5c(\u963b\u585e\u7b49\u5f85)\u540e\uff0c\u4e0d\u7528\u7b49IO\u64cd\u4f5c\u7ed3\u675f\uff0c\u53ef\u4ee5\u7ee7\u7eed\u5176\u5b83\u64cd\u4f5c;\u505a\u5176\u4ed6\u4e8b\u60c5\uff0c\u5f53IO\u64cd\u4f5c\u7ed3\u675f\u65f6\uff0c\u4f1a\u5f97\u5230\u901a\u77e5\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3002\u5f02\u6b65IO\u7f16\u7a0b\u662f\u5b9e\u73b0\u5e76\u53d1\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u9002\u7528\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1<\/p>\n<p>Python \u6a21\u5757 asyncio \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f02\u6b65\u7f16\u7a0b\u6846\u67b6\uff0c\u5168\u5c40\u7684\u6d41\u7a0b\u56fe\u5927\u81f4\u5982\u4e0b:<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/1024536-20220805174702308-1905046642.png\" alt=\"image\" \/><br \/>\n\u4e0b\u9762\u5bf9\u6bcf\u4e2a\u51fd\u6570\u90fd\u4ece\u4ee3\u7801\u5c42\u9762\u8fdb\u884c\u4ecb\u7ecd<\/p>\n<ul>\n<li><code>async<\/code>: \u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5(\u51fd\u6570),\u8fd9\u4e2a\u65b9\u6cd5\u5728\u540e\u9762\u7684\u8c03\u7528\u4e2d\u4e0d\u4f1a\u88ab\u7acb\u5373\u6267\u884c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61;<\/li>\n<\/ul>\n<pre><code class=\"language-python\">async def test(): print(&#039;hello \u5f02\u6b65&#039;)\n    test() # \u8c03\u7528\u5f02\u6b65\u51fd\u6570\n\n# \u8f93\u51fa\uff1aRuntimeWarning: coroutine &#039;test&#039; was never awaited<\/code><\/pre>\n<ul>\n<li><code>coroutine<\/code>: \u534f\u7a0b\u5bf9\u8c61,\u4e5f\u53ef\u4ee5\u5c06\u534f\u7a0b\u5bf9\u8c61\u6dfb\u52a0\u5230\u65f6\u95f4\u5faa\u73af\u4e2d,\u5b83\u4f1a\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u7528;<\/li>\n<\/ul>\n<pre><code class=\"language-python\">async def test(): \n    print(&#039;hello \u5f02\u6b65&#039;)\nc = test() # \u8c03\u7528\u5f02\u6b65\u51fd\u6570,\u5f97\u5230\u534f\u7a0b\u5bf9\u8c61--&gt;c \nprint(c)\n\n# \u8f93\u51fa:&lt;coroutine object test at 0x0000023FD05AA360&gt;<\/code><\/pre>\n<ul>\n<li><code>event_loop<\/code>: \u4e8b\u4ef6\u5faa\u73af,\u76f8\u5f53\u4e8e\u4e00\u4e2a\u65e0\u9650\u5faa\u73af,\u53ef\u4ee5\u628a\u4e00\u4e9b\u51fd\u6570\u6dfb\u52a0\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u4e2d,\u51fd\u6570\u4e0d\u4f1a\u7acb\u5373\u6267\u884c, \u800c\u662f\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u65f6\u5019,\u51fd\u6570\u5c31\u4f1a\u88ab\u5faa\u73af\u6267\u884c;<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import asyncio\n\nasync def test(): \n    print(&#039;hello \u5f02\u6b65&#039;)\nc = test() # \u8c03\u7528\u5f02\u6b65\u51fd\u6570,\u5f97\u5230\u534f\u7a0b\u5bf9\u8c61--&gt;c\nloop = asyncio.get_event_loop() # \u521b\u5efa\u4e8b\u4ef6\u5faa\u73af \nloop.run_until_complete(c) # \u628a\u534f\u7a0b\u5bf9\u8c61\u4e22\u7ed9\u5faa\u73af,\u5e76\u6267\u884c\u5f02\u6b65\u51fd\u6570\u5185\u90e8\u4ee3\u7801\n\n# \u8f93\u51fa:hello \u5f02\u6b65<\/code><\/pre>\n<ul>\n<li><code>await<\/code>: \u7528\u6765\u6302\u8d77\u963b\u585e\u65b9\u6cd5\u7684\u6267\u884c;<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import asyncio\ndef running1():\n    async def test1():\n        print(&#039;1&#039;)\n        await test2()\n        print(&#039;2&#039;)\n    async def test2():\n        print(&#039;3&#039;)\n        print(&#039;4&#039;)\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(test1())\nif __name__ == &#039;__main__&#039;:\n    running1()<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>1\n3\n4\n2\n\nProcess finished with exit code 0\n<\/code><\/pre>\n<ul>\n<li><code>task<\/code>: \u4efb\u52a1,\u5bf9\u534f\u7a0b\u5bf9\u8c61\u7684\u8fdb\u4e00\u6b65\u5c01\u88c5,\u5305\u542b\u4efb\u52a1\u7684\u5404\u4e2a\u72b6\u6001;<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import asyncio\n\nasync def test(): \n    print(&#039;hello \u5f02\u6b65&#039;)\nc = test() # \u8c03\u7528\u5f02\u6b65\u51fd\u6570,\u5f97\u5230\u534f\u7a0b\u5bf9\u8c61--&gt;c\nloop = asyncio.get_event_loop() # \u521b\u5efa\u4e8b\u4ef6\u5faa\u73af \ntask = loop.create_task(c) # \u521b\u5efatask\u4efb\u52a1 \nprint(task)\nloop.run_until_complete(task) # \u6267\u884c\u4efb\u52a1\n\n# \u8f93\u51fa:\n# &lt;Task pending coro=&lt;test() running at D: \/xxxx.py&gt;&gt; # task\n# hello \u5f02\u6b65 # \u5f02\u6b65\u51fd\u6570\u5185\u90e8\u4ee3\u7801\u4e00\u6837\u6267\u884c\n<\/code><\/pre>\n<ul>\n<li><code>future<\/code>: \u4ee3\u8868\u4ee5\u540e\u6267\u884c\u6216\u8005\u6ca1\u6709\u6267\u884c\u7684\u4efb\u52a1,\u5b9e\u9645\u4e0a\u548c<code>task<\/code>\u6ca1\u6709\u672c\u8d28\u533a\u522b;\u8fd9\u91cc\u5c31\u4e0d\u505a\u4ee3\u7801\u5c55\u793a;<br \/>\n\u9996\u5148\u4f7f\u7528\u4e00\u822c\u65b9\u5f0f\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570:<\/li>\n<\/ul>\n<pre><code class=\"language-python\">def func(url): \n    print(f&#039;\u6b63\u5728\u5bf9{url}\u53d1\u8d77\u8bf7\u6c42:&#039;) \n    print(f&#039;\u8bf7\u6c42{url}\u6210\u529f!&#039;)\nfunc(&#039;www.baidu.com&#039;)\n<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code class=\"language-python\">\u6b63\u5728\u5bf9www.baidu.com\u53d1\u8d77\u8bf7\u6c42:\n\u8bf7\u6c42www.baidu.com\u6210\u529f\n<\/code><\/pre>\n<h1>\u57fa\u672c\u64cd\u4f5c<\/h1>\n<h2>\u521b\u5efa\u534f\u7a0b\u5bf9\u8c61<\/h2>\n<p>\u901a\u8fc7 <code>async<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u8c03\u7528\u5f02\u6b65\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002<\/p>\n<p>\u5f02\u6b65\u51fd\u6570\u5c31\u662f\u5728\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e2d\u6302\u8d77\uff0c\u53bb\u6267\u884c\u5176\u4ed6\u5f02\u6b65\u51fd\u6570\uff0c\u7b49\u5f85\u6302\u8d77\u6761\u4ef6<code>(time.sleep(n))<\/code>\u6d88\u5931\u540e\uff0c\u518d\u56de\u6765\u6267\u884c\uff0c\u63a5\u7740\u6211\u4eec\u6765\u4fee\u6539\u4e0a\u8ff0\u4ee3\u7801:<\/p>\n<pre><code class=\"language-python\">async def func(url): \n    print(f&#039;\u6b63\u5728\u5bf9{url}\u53d1\u8d77\u8bf7\u6c42:&#039;) \n    print(f&#039;\u8bf7\u6c42{url}\u6210\u529f!&#039;)\nfunc(&#039;www.baidu.com&#039;)<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b:<\/p>\n<pre><code class=\"language-python\">RuntimeWarning: coroutine &#039;func&#039; was never awaited<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f\u4e4b\u524d\u63d0\u5230\u7684,\u4f7f\u7528async\u5173\u952e\u5b57\u4f7f\u5f97\u51fd\u6570\u8c03\u7528\u5f97\u5230\u4e86\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61,\u534f\u7a0b\u4e0d\u80fd\u76f4\u63a5\u8fd0\u884c\uff0c\u9700\u8981\u628a\u534f\u7a0b \u52a0\u5165\u5230\u4e8b\u4ef6\u5faa\u73af\u4e2d\uff0c\u7531\u540e\u8005\u5728\u9002\u5f53\u7684\u65f6\u5019\u8c03\u7528\u534f\u7a0b;<\/p>\n<h2>\u521b\u5efatask\u4efb\u52a1\u5bf9\u8c61<\/h2>\n<p>task\u4efb\u52a1\u5bf9\u8c61\u662f\u5bf9\u534f\u7a0b\u5bf9\u8c61\u7684\u8fdb\u4e00\u6b65\u5c01\u88c5;<\/p>\n<pre><code class=\"language-python\">import asyncio\nasync def func(url): \n    print(f&#039;\u6b63\u5728\u5bf9{url}\u53d1\u8d77\u8bf7\u6c42:&#039;) \n    print(f&#039;\u8bf7\u6c42{url}\u6210\u529f!&#039;)\nc = func(&#039;www.baidu.com&#039;) # \u51fd\u6570\u8c03\u7528\u7684\u5199\u6210\u5bf9\u8c61--&gt; c\nloop = asyncio.get_event_loop() # \u521b\u5efa\u4e00\u4e2a\u65f6\u95f4\u5faa\u73af\u5bf9\u8c61 \ntask = loop.create_task(c) \nloop.run_until_complete(task) # \u6ce8\u518c\u52a0\u542f\u52a8 \nprint(task)<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b:<\/p>\n<pre><code class=\"language-python\">\u6b63\u5728\u5bf9www.baidu.com\u53d1\u8d77\u8bf7\u6c42:\n\u8bf7\u6c42www.baidu.com\u6210\u529f!\n&lt;Task finished coro=&lt;func() done, defined at D:\/data_\/test.py:10&gt; result=None&gt;<\/code><\/pre>\n<h2>future\u7684\u4f7f\u7528<\/h2>\n<p>\u524d\u9762\u6211\u4eec\u63d0\u53ca\u5230future\u548ctask\u6ca1\u6709\u672c\u8d28\u533a\u522b<\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def func(url):\n   print(f&#039;\u6b63\u5728\u5bf9{url}\u53d1\u8d77\u8bf7\u6c42:&#039;)\n   print(f&#039;\u8bf7\u6c42{url}\u6210\u529f!&#039;)\nc = func(&#039;www.baidu.com&#039;) # \u51fd\u6570\u8c03\u7528\u7684\u5199\u6210\u5bf9\u8c61--&gt; c\n\nloop = asyncio.get_event_loop() # \u521b\u5efa\u4e00\u4e2a\u65f6\u95f4\u5faa\u73af\u5bf9\u8c61\nfuture_task = asyncio.ensure_future(c)\nprint(future_task,&#039;\u672a\u6267\u884c&#039;)\nloop.run_until_complete(future_task) # \u6ce8\u518c\u52a0\u542f\u52a8\nprint(future_task,&#039;\u6267\u884c\u5b8c\u4e86&#039;)<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b:<\/p>\n<pre><code class=\"language-python\">&lt;Task pending coro=&lt;func() running at D:\/data\/test.py:10&gt;&gt;\u672a\u6267\u884c\n\u6b63\u5728\u5bf9www.baidu.com\u53d1\u8d77\u8bf7\u6c42:\n\u8bf7\u6c42www.baidu.com\u6210\u529f!\n&lt;Task finished coro=&lt;func() done, defined at D:\/data\/test.py:10&gt; result=None&gt; \u6267\u884c\u5b8c\u4e86<\/code><\/pre>\n<h2>await\u5173\u952e\u5b57\u7684\u4f7f\u7528<\/h2>\n<p>\u5728\u5f02\u6b65\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528await\u5173\u952e\u5b57\uff0c\u9488\u5bf9\u8017\u65f6\u7684\u64cd\u4f5c(\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49IO\u64cd\u4f5c)\u8fdb\u884c\u6302\u8d77\uff0c\u6bd4\u5982\u5f02\u6b65\u7a0b\u5e8f\u6267\u884c\u5230\u67d0\u4e00\u6b65\u65f6\u9700\u8981\u5f88\u957f\u65f6\u95f4\u7684\u7b49\u5f85\uff0c\u5c31\u5c06\u6b64\u6302\u8d77\uff0c\u53bb\u6267\u884c\u5176\u4ed6\u5f02\u6b65\u51fd\u6570<\/p>\n<pre><code class=\"language-python\">import asyncio, time\nasync def do_some_work(n): #\u4f7f\u7528async\u5173\u952e\u5b57\u5b9a\u4e49\u5f02\u6b65\u51fd\u6570\n    print(&#039;\u7b49\u5f85:{}\u79d2&#039;.format(n))\n    await asyncio.sleep(n) #\u4f11\u7720\u4e00\u6bb5\u65f6\u95f4 \n    return &#039;{}\u79d2\u540e\u8fd4\u56de\u7ed3\u675f\u8fd0\u884c&#039;.format(n)\nstart_time = time.time() #\u5f00\u59cb\u65f6\u95f4\ncoro = do_some_work(2)\nloop = asyncio.get_event_loop() # \u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u5bf9\u8c61 \nloop.run_until_complete(coro)\nprint(&#039;\u8fd0\u884c\u65f6\u95f4: &#039;, time.time() - start_time)<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>\u7b49\u5f85:2\u79d2\n\u8fd0\u884c\u65f6\u95f4:  2.001708984375<\/code><\/pre>\n<h1>\u591a\u4efb\u52a1\u534f\u7a0b<\/h1>\n<p>\u4efb\u52a1(Task)\u5bf9\u8c61\u7528\u4e8e\u5c01\u88c5\u534f\u7a0b\u5bf9\u8c61\uff0c\u4fdd\u5b58\u4e86\u534f\u7a0b\u8fd0\u884c\u540e\u7684\u72b6\u6001\uff0c\u4f7f\u7528 <code>run_until_complete()<\/code> \u65b9\u6cd5\u5c06\u4efb\u52a1\u6ce8\u518c\u5230\u4e8b\u4ef6\u5faa\u73af;<\/p>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u591a\u4efb\u52a1,\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u540c\u65f6\u6ce8\u518c\u591a\u4e2a\u4efb\u52a1\u7684\u5217\u8868,\u53ef\u4ee5\u4f7f\u7528 <code>run_until_complete(asyncio.wait(tasks))<\/code>\uff0c<\/p>\n<p>\u8fd9\u91cc\u7684<code>tasks<\/code>,\u8868\u793a\u4e00\u4e2a\u4efb\u52a1\u5e8f\u5217(\u901a\u5e38\u4e3a\u5217\u8868)<\/p>\n<p>\u6ce8\u518c\u591a\u4e2a\u4efb\u52a1\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>run_until_complete(asyncio. gather(*tasks))<\/code><\/p>\n<pre><code class=\"language-python\">import asyncio, time\nasync def do_some_work(i, n): #\u4f7f\u7528async\u5173\u952e\u5b57\u5b9a\u4e49\u5f02\u6b65\u51fd\u6570\n    print(&#039;\u4efb\u52a1{}\u7b49\u5f85: {}\u79d2&#039;.format(i, n))\n    await asyncio.sleep(n) #\u4f11\u7720\u4e00\u6bb5\u65f6\u95f4\n    return &#039;\u4efb\u52a1{}\u5728{}\u79d2\u540e\u8fd4\u56de\u7ed3\u675f\u8fd0\u884c&#039;.format(i, n)\nstart_time = time.time() #\u5f00\u59cb\u65f6\u95f4\ntasks = [asyncio.ensure_future(do_some_work(1, 2)),\n        asyncio.ensure_future(do_some_work(2, 1)),\n        asyncio.ensure_future(do_some_work(3, 3))]\nloop = asyncio.get_event_loop()\nloop.run_until_complete(asyncio.wait(tasks))\nfor task in tasks:\n    print(&#039;\u4efb\u52a1\u6267\u884c\u7ed3\u679c: &#039;, task.result())\nprint(&#039;\u8fd0\u884c\u65f6\u95f4: &#039;, time.time() - start_time)<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-python\">\u4efb\u52a11\u7b49\u5f85: 2\u79d2\n\u4efb\u52a12\u7b49\u5f85: 1\u79d2\n\u4efb\u52a13\u7b49\u5f85: 3\u79d2\n\u4efb\u52a1\u6267\u884c\u7ed3\u679c:  \u4efb\u52a11\u57282\u79d2\u540e\u8fd4\u56de\u7ed3\u675f\u8fd0\u884c\n\u4efb\u52a1\u6267\u884c\u7ed3\u679c:  \u4efb\u52a12\u57281\u79d2\u540e\u8fd4\u56de\u7ed3\u675f\u8fd0\u884c\n\u4efb\u52a1\u6267\u884c\u7ed3\u679c:  \u4efb\u52a13\u57283\u79d2\u540e\u8fd4\u56de\u7ed3\u675f\u8fd0\u884c\n\u8fd0\u884c\u65f6\u95f4:  3.000883102416992<\/code><\/pre>\n<p>\u6765\u6e90\uff1a<a href=\"https:\/\/baijiahao.baidu.com\/s?id=1714015900283907482&amp;wfr=spider&amp;for=pc\">https:\/\/baijiahao.baidu.com\/s?id=1714015900283907482&amp;wfr=spider&amp;for=pc<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e86\u89e3\u4e86 Python \u5e76\u53d1\u7f16\u7a0b\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u4e4b\u540e,\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0b\u57fa\u4e8e asyncio \u7684\u5f02\u6b65IO\u7f16\u7a0b&#8211;\u534f\u7a0b \u534f\u7a0b\u7b80\u4ecb \u534f\u7a0b(Cor   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[268],"tags":[266,16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9250"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9250"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9250\/revisions"}],"predecessor-version":[{"id":9251,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9250\/revisions\/9251"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9250"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}