{"id":214,"date":"2026-07-22T15:48:20","date_gmt":"2026-07-22T07:48:20","guid":{"rendered":"https:\/\/yykcj.com\/?p=214"},"modified":"2026-07-22T15:48:20","modified_gmt":"2026-07-22T07:48:20","slug":"python%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%ef%bc%9aasyncio%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3%e4%b8%8e%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=214","title":{"rendered":"Python\u5f02\u6b65\u7f16\u7a0b\uff1aasyncio\u6df1\u5165\u7406\u89e3\u4e0e\u5b9e\u6218"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u5728I\/O\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\uff0cPython\u7684\u5f02\u6b65\u7f16\u7a0b\uff08asyncio\uff09\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u7136\u800c\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u5bf9asyncio\u7684\u7406\u89e3\u505c\u7559\u5728\u8868\u9762\uff0c\u5bfc\u81f4\u4ee3\u7801\u6548\u7387\u4f4e\u4e0b\u6216\u51fa\u73b0\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ece\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u548cFuture\u51fa\u53d1\uff0c\u6df1\u5165\u5256\u6790asyncio\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>1. \u4e8b\u4ef6\u5faa\u73af\uff1a\u5f02\u6b65\u7684\u5f15\u64ce<\/h2>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662fasyncio\u7684\u6838\u5fc3\uff0c\u5b83\u8d1f\u8d23\u8c03\u5ea6\u548c\u6267\u884c\u534f\u7a0b\u3002Python 3.10+\u63a8\u8350\u4f7f\u7528<code>asyncio.run()<\/code>\u6765\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<pre><code class=\"language-python\">\nimport asyncio\n\nasync def main():\n    print(&#x27;Hello&#x27;)\n    await asyncio.sleep(1)\n    print(&#x27;World&#x27;)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>asyncio.run()<\/code>\u4f1a\u521b\u5efa\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u5e76\u5728\u5b8c\u6210\u540e\u5173\u95ed\u5b83\u3002\u4e0d\u8981\u624b\u52a8\u8c03\u7528<code>loop.close()<\/code>\uff0c\u9664\u975e\u4f60\u6709\u7279\u6b8a\u9700\u6c42\u3002<\/p>\n<h3>\u5e95\u5c42\u4e8b\u4ef6\u5faa\u73af\u64cd\u4f5c<\/h3>\n<p>\u5982\u679c\u9700\u8981\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528<code>loop = asyncio.new<em>event<\/em>loop()<\/code>\u548c<code>loop.run<em>until<\/em>complete()<\/code>\uff0c\u4f46\u901a\u5e38\u4e0d\u63a8\u8350\u3002<\/p>\n<h2>2. \u534f\u7a0b\uff1aasync\/await\u8bed\u6cd5<\/h2>\n<p>\u534f\u7a0b\u662f\u901a\u8fc7<code>async def<\/code>\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u8c03\u7528\u534f\u7a0b\u51fd\u6570\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def fetch_data():\n    print(&quot;\u5f00\u59cb\u83b7\u53d6\u6570\u636e&quot;)\n    await asyncio.sleep(2)\n    print(&quot;\u6570\u636e\u83b7\u53d6\u5b8c\u6210&quot;)\n    return {&quot;data&quot;: 42}\n\n# \u6b63\u786e\u6267\u884c\nresult = asyncio.run(fetch_data())\nprint(result)  # \u8f93\u51fa: {&#x27;data&#x27;: 42}\n<\/code><\/pre>\n<p><strong>\u5e38\u89c1\u9519\u8bef<\/strong>\uff1a\u5fd8\u8bb0<code>await<\/code>\uff0c\u5bfc\u81f4\u534f\u7a0b\u5bf9\u8c61\u672a\u88ab\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-python\">\n# \u9519\u8bef\u793a\u4f8b\nasync def main():\n    fetch_data()  # \u534f\u7a0b\u5bf9\u8c61\u88ab\u521b\u5efa\u4f46\u672a\u6267\u884c\n\nasyncio.run(main())\n<\/code><\/pre>\n<h2>3. \u4efb\u52a1\uff1a\u5e76\u53d1\u6267\u884c\u534f\u7a0b<\/h2>\n<p>\u4efb\u52a1\u662f\u534f\u7a0b\u7684\u5c01\u88c5\uff0c\u7528\u4e8e\u5e76\u53d1\u6267\u884c\u3002\u4f7f\u7528<code>asyncio.create_task()<\/code>\u521b\u5efa\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def task1():\n    await asyncio.sleep(2)\n    return &quot;Task1 done&quot;\n\nasync def task2():\n    await asyncio.sleep(1)\n    return &quot;Task2 done&quot;\n\nasync def main():\n    t1 = asyncio.create_task(task1())\n    t2 = asyncio.create_task(task2())\n    \n    # \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n    results = await asyncio.gather(t1, t2)\n    print(results)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa<\/strong>\uff1a<code>['Task1 done', 'Task2 done']<\/code>\uff0c\u603b\u8017\u65f6\u7ea62\u79d2\uff08\u5e76\u53d1\u6267\u884c\uff09\u3002<\/p>\n<h3>\u4efb\u52a1\u53d6\u6d88\u4e0e\u8d85\u65f6<\/h3>\n<p>\u4f7f\u7528<code>asyncio.wait_for()<\/code>\u8bbe\u7f6e\u8d85\u65f6\uff0c\u6216<code>task.cancel()<\/code>\u53d6\u6d88\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def slow_operation():\n    await asyncio.sleep(10)\n    return &quot;\u5b8c\u6210&quot;\n\nasync def main():\n    try:\n        result = await asyncio.wait_for(slow_operation(), timeout=3)\n    except asyncio.TimeoutError:\n        print(&quot;\u64cd\u4f5c\u8d85\u65f6&quot;)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u8d85\u65f6\u540e\u4efb\u52a1\u4f1a\u88ab\u53d6\u6d88\uff0c\u5e76\u629b\u51fa<code>CancelledError<\/code>\u3002\u5982\u679c\u9700\u8981\u6e05\u7406\u8d44\u6e90\uff0c\u53ef\u4ee5\u6355\u83b7<code>CancelledError<\/code>\u3002<\/p>\n<h2>4. Future\uff1a\u5e95\u5c42\u5f02\u6b65\u7ed3\u679c<\/h2>\n<p>Future\u662f\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u7ed3\u679c\u7684\u7c7b\uff0c\u7c7b\u4f3c\u4e8eJavaScript\u7684Promise\u3002\u901a\u5e38\u6211\u4eec\u4e0d\u76f4\u63a5\u4f7f\u7528Future\uff0c\u800c\u662f\u901a\u8fc7<code>ensure<em>future()<\/code>\u6216<code>loop.create<\/em>future()<\/code>\u521b\u5efa\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def set_future(fut):\n    await asyncio.sleep(1)\n    fut.set_result(&quot;Future resolved&quot;)\n\nasync def main():\n    loop = asyncio.get_running_loop()\n    fut = loop.create_future()\n    asyncio.create_task(set_future(fut))\n    result = await fut\n    print(result)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u7406\u89e3<\/strong>\uff1a<code>await fut<\/code>\u4f1a\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff0c\u76f4\u5230Future\u88ab\u8bbe\u7f6e\u7ed3\u679c\u3002<\/p>\n<h2>5. \u5b9e\u6218\uff1a\u5f02\u6b65HTTP\u8bf7\u6c42<\/h2>\n<p>\u4f7f\u7528<code>aiohttp<\/code>\u5e93\u8fdb\u884c\u5f02\u6b65HTTP\u8bf7\u6c42\uff08\u9700\u5b89\u88c5\uff1a<code>pip install aiohttp<\/code>\uff09\u3002<\/p>\n<pre><code class=\"language-python\">\nimport aiohttp\nimport asyncio\n\nasync def fetch(session, url):\n    async with session.get(url) as response:\n        return await response.text()\n\nasync def main():\n    urls = [&quot;https:\/\/httpbin.org\/delay\/1&quot;, &quot;https:\/\/httpbin.org\/delay\/2&quot;, &quot;https:\/\/httpbin.org\/delay\/3&quot;]\n    async with aiohttp.ClientSession() as session:\n        tasks = [asyncio.create_task(fetch(session, url)) for url in urls]\n        pages = await asyncio.gather(*tasks)\n        print(f&quot;\u83b7\u53d6\u4e86 {len(pages)} \u4e2a\u9875\u9762&quot;)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u6027\u80fd\u5bf9\u6bd4<\/strong>\uff1a\u540c\u6b65\u7248\u672c\u9700\u8981\u7ea66\u79d2\uff0c\u5f02\u6b65\u7248\u672c\u4ec5\u9700\u7ea63\u79d2\uff08\u53d6\u51b3\u4e8e\u7f51\u7edc\u5ef6\u8fdf\uff09\u3002<\/p>\n<h2>6. \u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>\u9677\u96311\uff1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af<\/h3>\n<p>\u5728\u534f\u7a0b\u4e2d\u4e0d\u8981\u4f7f\u7528<code>time.sleep()<\/code>\uff0c\u5b83\u4f1a\u963b\u585e\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002\u4f7f\u7528<code>asyncio.sleep()<\/code>\u3002<\/p>\n<pre><code class=\"language-python\">\n# \u9519\u8bef\nasync def bad():\n    time.sleep(1)  # \u963b\u585e\u4e8b\u4ef6\u5faa\u73af\n\n# \u6b63\u786e\nasync def good():\n    await asyncio.sleep(1)\n<\/code><\/pre>\n<h3>\u9677\u96312\uff1a\u5fd8\u8bb0await<\/h3>\n<p>\u786e\u4fdd\u6240\u6709\u534f\u7a0b\u90fd\u88ab<code>await<\/code>\u6216\u4f5c\u4e3a\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n<h3>\u9677\u96313\uff1a\u5171\u4eab\u72b6\u6001<\/h3>\n<p>\u5f02\u6b65\u4ee3\u7801\u4e2d\u5171\u4eab\u53ef\u53d8\u72b6\u6001\u53ef\u80fd\u5bfc\u81f4\u7ade\u6001\u6761\u4ef6\u3002\u4f7f\u7528<code>asyncio.Lock<\/code>\u4fdd\u62a4\u4e34\u754c\u533a\u3002<\/p>\n<pre><code class=\"language-python\">\nlock = asyncio.Lock()\n\nasync def critical_section():\n    async with lock:\n        # \u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\n        pass\n<\/code><\/pre>\n<h3>\u6700\u4f73\u5b9e\u8df5\uff1a\u4f7f\u7528asyncio.run()<\/h3>\n<p>\u59cb\u7ec8\u4f7f\u7528<code>asyncio.run()<\/code>\u4f5c\u4e3a\u5165\u53e3\uff0c\u5b83\u8d1f\u8d23\u521b\u5efa\u548c\u5173\u95ed\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h3>\u6700\u4f73\u5b9e\u8df5\uff1a\u5408\u7406\u4f7f\u7528gather\u4e0ewait<\/h3>\n<ul>\n<li><code>asyncio.gather()<\/code>\uff1a\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff0c\u5982\u679c\u67d0\u4e2a\u4efb\u52a1\u5f02\u5e38\uff0c\u4f1a\u7acb\u5373\u629b\u51fa\u3002<\/li>\n<li><code>asyncio.wait()<\/code>\uff1a\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u7b49\u5f85\u6240\u6709\u3001\u7b2c\u4e00\u4e2a\u5b8c\u6210\u3001\u7b2c\u4e00\u4e2a\u5f02\u5e38\u7b49\u3002<\/li>\n<\/ul>\n<h2>7. \u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86asyncio\u7684\u6838\u5fc3\u6982\u5ff5\uff1a\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u548cFuture\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5f02\u6b65HTTP\u8bf7\u6c42\u3002\u5173\u952e\u8981\u70b9\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>async\/await<\/code>\u5b9a\u4e49\u534f\u7a0b<\/li>\n<li>\u7528<code>asyncio.create_task()<\/code>\u5e76\u53d1\u6267\u884c<\/li>\n<li>\u907f\u514d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af<\/li>\n<li>\u4f7f\u7528<code>asyncio.run()<\/code>\u7ba1\u7406\u4e8b\u4ef6\u5faa\u73af<\/li>\n<\/ul>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong>\uff1a<\/p>\n<ul>\n<li>asyncio\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/docs.python.org\/zh-cn\/3\/library\/asyncio.html<\/li>\n<li>\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff1a\u56de\u8c03\u3001Future\u3001\u534f\u7a0b\u5bf9\u6bd4<\/li>\n<\/ul>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u6df1\u5165\u7406\u89e3asyncio\uff0c\u5199\u51fa\u9ad8\u6548\u53ef\u9760\u7684\u5f02\u6b65Python\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u5256\u6790Python asyncio\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5305\u62ec\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u548cFuture\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5c55\u793a\u5982\u4f55\u7f16\u5199\u9ad8\u6548\u5f02\u6b65\u4ee3\u7801\uff0c\u907f\u514d\u5e38\u89c1\u9677\u9631\u3002\u9002\u5408\u6709Python\u57fa\u7840\u7684\u5f00\u53d1\u8005\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[102,86,146,104,103],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-asyncio","tag-python","tag-146","tag-104","tag-103"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=214"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/214\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}