{"id":245,"date":"2026-09-07T09:51:17","date_gmt":"2026-09-07T01:51:17","guid":{"rendered":"https:\/\/yykcj.com\/?p=245"},"modified":"2026-09-07T09:51:17","modified_gmt":"2026-09-07T01:51:17","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%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=245","title":{"rendered":"Python\u5f02\u6b65\u7f16\u7a0b\uff1aasyncio\u6df1\u5165\u7406\u89e3\u4e0e\u5b9e\u6218\u6307\u5357"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5728Python\u5f00\u53d1\u4e2d\uff0cI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\uff09\u5e38\u5e38\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002\u4f20\u7edf\u7684\u540c\u6b65\u4ee3\u7801\u5728\u7b49\u5f85I\/O\u65f6\u4f1a\u963b\u585e\u6574\u4e2a\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\u3002\u867d\u7136\u591a\u7ebf\u7a0b\u53ef\u4ee5\u7f13\u89e3\uff0c\u4f46\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\u548cGIL\u9650\u5236\u8ba9Python\u7684\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e0a\u529b\u4e0d\u4ece\u5fc3\u3002\u800c\u5f02\u6b65\u7f16\u7a0b\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u548c\u534f\u7a0b\uff0c\u8ba9\u5355\u4e2a\u7ebf\u7a0b\u5728\u7b49\u5f85I\/O\u65f6\u5207\u6362\u5230\u5176\u4ed6\u4efb\u52a1\uff0c\u6781\u5927\u63d0\u5347\u5e76\u53d1\u6027\u80fd\u3002Python\u7684<code>asyncio<\/code>\u5e93\u81ea3.4\u5f15\u5165\uff0c\u5df2\u6210\u4e3a\u5f02\u6b65\u7f16\u7a0b\u7684\u6807\u51c6\u3002\u672c\u6587\u5c06\u4ece\u57fa\u7840\u5230\u5b9e\u6218\uff0c\u6df1\u5165\u5256\u6790asyncio\u7684\u6838\u5fc3\uff0c\u5e76\u5206\u4eab\u8e29\u5751\u7ecf\u9a8c\u4e0e\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>1. \u6838\u5fc3\u6982\u5ff5\uff1a\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u4e0eFuture<\/h2>\n<h3>1.1 \u4e8b\u4ef6\u5faa\u73af\uff08Event Loop\uff09<\/h3>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662fasyncio\u7684\u5fc3\u810f\uff0c\u5b83\u8d1f\u8d23\u8c03\u5ea6\u534f\u7a0b\u3001\u5904\u7406I\/O\u4e8b\u4ef6\u3002\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\u4e00\u4e2a\u4e0d\u65ad\u5faa\u73af\u7684\u8c03\u5ea6\u5668\uff1a\u5b83\u68c0\u67e5\u662f\u5426\u6709\u5c31\u7eea\u7684\u4efb\u52a1\uff0c\u6267\u884c\u5b83\u4eec\uff0c\u5e76\u5904\u7406\u5b83\u4eec\u4ea7\u751f\u7684I\/O\u4e8b\u4ef6\u3002<\/p>\n<p>\u5728Python 3.7+\uff0c\u63a8\u8350\u4f7f\u7528<code>asyncio.run()<\/code>\u6765\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\uff0c\u5b83\u4f1a\u81ea\u52a8\u521b\u5efa\u5e76\u5173\u95ed\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<h3>1.2 \u534f\u7a0b\uff08Coroutine\uff09<\/h3>\n<p>\u534f\u7a0b\u662f\u5f02\u6b65\u51fd\u6570\uff0c\u4f7f\u7528<code>async def<\/code>\u5b9a\u4e49\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\u534f\u7a0b\u9700\u8981\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u624d\u80fd\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def my_coroutine():\n    return 42\n\ncoro = my_coroutine()  # \u4e0d\u4f1a\u6267\u884c\uff0c\u8fd4\u56de\u534f\u7a0b\u5bf9\u8c61\n# \u9700\u8981await\u6216\u5305\u88c5\u6210\u4efb\u52a1\u624d\u80fd\u6267\u884c\n<\/code><\/pre>\n<h3>1.3 \u4efb\u52a1\uff08Task\uff09<\/h3>\n<p>\u4efb\u52a1\u662f\u5bf9\u534f\u7a0b\u7684\u5c01\u88c5\uff0c\u7528\u4e8e\u5e76\u53d1\u8c03\u5ea6\u3002\u521b\u5efa\u4efb\u52a1\u540e\uff0c\u534f\u7a0b\u4f1a\u5c3d\u5feb\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u6267\u884c\u3002\u4f7f\u7528<code>asyncio.create_task()<\/code>\u521b\u5efa\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    task = asyncio.create_task(my_coroutine())\n    result = await task\n    print(result)\n\nasyncio.run(main())\n<\/code><\/pre>\n<h3>1.4 Future<\/h3>\n<p>Future\u662f\u66f4\u4f4e\u7ea7\u7684\u5bf9\u8c61\uff0c\u8868\u793a\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u7ed3\u679c\u3002\u4efb\u52a1\u7ee7\u627f\u81eaFuture\u3002\u901a\u5e38\u6211\u4eec\u4e0d\u9700\u8981\u76f4\u63a5\u64cd\u4f5cFuture\uff0c\u4f46\u7406\u89e3\u5b83\u6709\u52a9\u4e8e\u6df1\u5165\u7406\u89e3asyncio\u3002<\/p>\n<h2>2. \u5b9e\u6218\uff1a\u7f16\u5199\u5f02\u6b65HTTP\u5ba2\u6237\u7aef<\/h2>\n<p>\u8ba9\u6211\u4eec\u7528asyncio\u548c<code>aiohttp<\/code>\u7f16\u5199\u4e00\u4e2a\u5e76\u53d1\u4e0b\u8f7d\u591a\u4e2a\u7f51\u9875\u7684\u793a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\u5b89\u88c5aiohttp\uff1a<\/p>\n<pre><code class=\"language-bash\">\npip install aiohttp\n<\/code><\/pre>\n<p>\u7136\u540e\u7f16\u5199\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">\nimport asyncio\nimport aiohttp\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 = [\n        &#x27;https:\/\/httpbin.org\/get&#x27;,\n        &#x27;https:\/\/httpbin.org\/ip&#x27;,\n        &#x27;https:\/\/httpbin.org\/user-agent&#x27;\n    ]\n    async with aiohttp.ClientSession() as session:\n        tasks = [asyncio.create_task(fetch(session, url)) for url in urls]\n        results = await asyncio.gather(*tasks)\n        for result in results:\n            print(result[:100])\n\nasyncio.run(main())\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86<code>asyncio.gather()<\/code>\u6765\u5e76\u53d1\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u5b83\u4f1a\u5728\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u540e\u8fd4\u56de\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<h2>3. \u63a7\u5236\u5e76\u53d1\uff1a\u4fe1\u53f7\u91cf\u4e0e\u9650\u5236<\/h2>\n<p>\u76f4\u63a5\u521b\u5efa\u5927\u91cf\u4efb\u52a1\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u8017\u5c3d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>asyncio.Semaphore<\/code>\u6765\u9650\u5236\u5e76\u53d1\u6570\u3002<\/p>\n<pre><code class=\"language-python\">\nimport asyncio\nimport aiohttp\n\nasync def fetch(session, url, semaphore):\n    async with semaphore:\n        async with session.get(url) as response:\n            return await response.text()\n\nasync def main():\n    urls = [f&#x27;https:\/\/httpbin.org\/get?i={i}&#x27; for i in range(100)]\n    semaphore = asyncio.Semaphore(10)  # \u6700\u591a\u540c\u65f610\u4e2a\u8bf7\u6c42\n    async with aiohttp.ClientSession() as session:\n        tasks = [asyncio.create_task(fetch(session, url, semaphore)) for url in urls]\n        results = await asyncio.gather(*tasks)\n    print(f&#x27;\u4e0b\u8f7d\u4e86{len(results)}\u4e2a\u9875\u9762&#x27;)\n\nasyncio.run(main())\n<\/code><\/pre>\n<h2>4. \u8d85\u65f6\u63a7\u5236\u4e0e\u53d6\u6d88\u4efb\u52a1<\/h2>\n<p>\u5f02\u6b65\u64cd\u4f5c\u53ef\u80fd\u6c38\u8fdc\u4e0d\u8fd4\u56de\uff0c\u6240\u4ee5\u8d85\u65f6\u63a7\u5236\u81f3\u5173\u91cd\u8981\u3002<code>asyncio.wait_for<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u3002<\/p>\n<pre><code class=\"language-python\">\nimport asyncio\n\nasync def slow_operation():\n    await asyncio.sleep(10)\n    return &#x27;done&#x27;\n\nasync def main():\n    try:\n        result = await asyncio.wait_for(slow_operation(), timeout=2)\n        print(result)\n    except asyncio.TimeoutError:\n        print(&#x27;\u64cd\u4f5c\u8d85\u65f6&#x27;)\n\nasyncio.run(main())\n<\/code><\/pre>\n<p>\u4efb\u52a1\u53d6\u6d88\uff1a\u6211\u4eec\u53ef\u4ee5\u8c03\u7528<code>task.cancel()<\/code>\u6765\u53d6\u6d88\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    task = asyncio.create_task(slow_operation())\n    await asyncio.sleep(1)\n    task.cancel()\n    try:\n        await task\n    except asyncio.CancelledError:\n        print(&#x27;\u4efb\u52a1\u88ab\u53d6\u6d88&#x27;)\n<\/code><\/pre>\n<h2>5. \u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>\u9677\u96311\uff1a\u5728\u5f02\u6b65\u4ee3\u7801\u4e2d\u8c03\u7528\u540c\u6b65\u963b\u585e\u51fd\u6570<\/h3>\n<p>\u5982\u679c\u5728\u534f\u7a0b\u4e2d\u8c03\u7528<code>time.sleep()<\/code>\u6216<code>requests.get()<\/code>\uff0c\u4f1a\u963b\u585e\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p><strong>\u9519\u8bef\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\">\nimport time\nimport asyncio\n\nasync def bad_coroutine():\n    time.sleep(1)  # \u963b\u585e\uff01\n<\/code><\/pre>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>asyncio.sleep()<\/code>\u6216<code>await loop.run<em>in<\/em>executor()<\/code>\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def good_coroutine():\n    await asyncio.sleep(1)  # \u975e\u963b\u585e\n\n# \u5bf9\u4e8e\u5fc5\u987b\u7684\u540c\u6b65\u963b\u585e\u51fd\u6570\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\nimport asyncio\nimport time\n\nasync def main():\n    loop = asyncio.get_running_loop()\n    await loop.run_in_executor(None, time.sleep, 1)  # \u5728\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\n<\/code><\/pre>\n<h3>\u9677\u96312\uff1a\u5fd8\u8bb0await\u534f\u7a0b<\/h3>\n<p>\u5982\u679c\u5fd8\u8bb0<code>await<\/code>\uff0c\u534f\u7a0b\u4e0d\u4f1a\u6267\u884c\uff0c\u53ea\u4f1a\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002\u8fd9\u5e38\u5bfc\u81f4\u4efb\u52a1\u672a\u8fd0\u884c\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    coro = my_coroutine()  # \u6ca1\u6709await\n    print(&#x27;\u534f\u7a0b\u672a\u6267\u884c&#x27;)\n<\/code><\/pre>\n<h3>\u9677\u96313\uff1a\u5728\u4e8b\u4ef6\u5faa\u73af\u5916\u521b\u5efa\u4efb\u52a1<\/h3>\n<p>\u5728\u4e8b\u4ef6\u5faa\u73af\u5916\u8c03\u7528<code>asyncio.create_task()<\/code>\u4f1a\u62a5\u9519\u3002\u786e\u4fdd\u5728\u534f\u7a0b\u5185\u90e8\u8c03\u7528\u3002<\/p>\n<h3>\u6700\u4f73\u5b9e\u8df5\uff1a\u4f7f\u7528<code>asyncio.run()<\/code>\u7edf\u4e00\u7ba1\u7406\u5faa\u73af<\/h3>\n<p>\u5bf9\u4e8e\u7b80\u5355\u811a\u672c\uff0c\u4f7f\u7528<code>asyncio.run()<\/code>\u5373\u53ef\u3002\u4f46\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5faa\u73af\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    # \u4e3b\u903b\u8f91\n    pass\n\nif __name__ == &#x27;__main__&#x27;:\n    asyncio.run(main())\n<\/code><\/pre>\n<h3>\u6700\u4f73\u5b9e\u8df5\uff1a\u4f18\u96c5\u5173\u95ed\u8d44\u6e90<\/h3>\n<p>\u4f7f\u7528<code>async with<\/code>\u7ba1\u7406\u8d44\u6e90\uff0c\u786e\u4fdd\u6e05\u7406\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    async with aiohttp.ClientSession() as session:\n        # \u4f7f\u7528session\n        pass\n<\/code><\/pre>\n<h2>6. \u603b\u7ed3\u4e0e\u5ef6\u4f38<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86asyncio\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u5c55\u793a\u4e86\u5982\u4f55\u7f16\u5199\u5e76\u53d1HTTP\u5ba2\u6237\u7aef\u3001\u63a7\u5236\u5e76\u53d1\u6570\u3001\u5904\u7406\u8d85\u65f6\u548c\u53d6\u6d88\u3002\u540c\u65f6\uff0c\u6211\u4eec\u6307\u51fa\u4e86\u5e38\u89c1\u7684\u9677\u9631\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p>\u8981\u6df1\u5165\u5b66\u4e60\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63a2\u7d22\u4ee5\u4e0b\u4e3b\u9898\uff1a<\/p>\n<ul>\n<li><code>asyncio.Queue<\/code> \u7528\u4e8e\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/li>\n<li>\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u5f02\u6b65\u8fed\u4ee3\u5668<\/li>\n<li>\u4f7f\u7528<code>asyncio.create<em>subprocess<\/em>shell<\/code>\u6267\u884c\u5b50\u8fdb\u7a0b<\/li>\n<li>\u4e0e\u591a\u7ebf\u7a0b\/\u591a\u8fdb\u7a0b\u7684\u7ec4\u5408\u4f7f\u7528<\/li>\n<\/ul>\n<p>\u5f02\u6b65\u7f16\u7a0b\u662f\u73b0\u4ee3Python\u5f00\u53d1\u7684\u91cd\u8981\u6280\u80fd\uff0c\u638c\u63e1\u5b83\u80fd\u8ba9\u4f60\u5199\u51fa\u66f4\u9ad8\u6548\u3001\u66f4\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u4f60\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u63a2\u8ba8Python asyncio\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5305\u62ec\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u4e0eFuture\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u7f16\u5199\u9ad8\u6548\u7684\u5f02\u6b65\u4ee3\u7801\uff0c\u540c\u65f6\u5206\u4eab\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[102,86,104,105,103],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-4","tag-asyncio","tag-python","tag-104","tag-105","tag-103"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/245","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=245"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}