{"id":196,"date":"2026-07-06T10:09:35","date_gmt":"2026-07-06T02:09:35","guid":{"rendered":"https:\/\/yykcj.com\/?p=196"},"modified":"2026-07-06T10:09:35","modified_gmt":"2026-07-06T02:09:35","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=196","title":{"rendered":"Python \u5f02\u6b65\u7f16\u7a0b\uff1aasyncio \u6df1\u5165\u7406\u89e3\u4e0e\u5b9e\u6218"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>Python \u7684 <code>asyncio<\/code> \u5e93\u4e3a\u7f16\u5199\u5e76\u53d1\u5f02\u6b65\u4ee3\u7801\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u7136\u800c\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u5bf9\u5176\u5185\u90e8\u673a\u5236\u7406\u89e3\u4e0d\u6df1\uff0c\u5bfc\u81f4\u96be\u4ee5\u7f16\u5199\u51fa\u9ad8\u6548\u7684\u5f02\u6b65\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 asyncio \u7684\u6838\u5fc3\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5e2e\u52a9\u4f60\u638c\u63e1\u5f02\u6b65\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>1. \u4e8b\u4ef6\u5faa\u73af\u4e0e\u534f\u7a0b<\/h2>\n<h3>1.1 \u4e8b\u4ef6\u5faa\u73af<\/h3>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f asyncio \u7684\u6838\u5fc3\uff0c\u5b83\u8d1f\u8d23\u8c03\u5ea6\u548c\u6267\u884c\u534f\u7a0b\u3002\u5728 Python 3.7+ \u4e2d\uff0c\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<h3>1.2 \u534f\u7a0b<\/h3>\n<p>\u534f\u7a0b\u662f\u901a\u8fc7 <code>async def<\/code> \u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u8c03\u7528\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002\u534f\u7a0b\u5bf9\u8c61\u9700\u8981\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u624d\u80fd\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def foo():\n    return 42\n\ncoro = foo()  # \u534f\u7a0b\u5bf9\u8c61\nresult = await coro  # \u7b49\u5f85\u7ed3\u679c\n<\/code><\/pre>\n<h2>2. \u4efb\u52a1 (Task)<\/h2>\n<p>\u4efb\u52a1\u7528\u4e8e\u5e76\u53d1\u6267\u884c\u534f\u7a0b\u3002\u901a\u8fc7 <code>asyncio.create_task()<\/code> \u53ef\u4ee5\u5c06\u534f\u7a0b\u5305\u88c5\u6210\u4efb\u52a1\uff0c\u5e76\u7acb\u5373\u5f00\u59cb\u8c03\u5ea6\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def say_after(delay, msg):\n    await asyncio.sleep(delay)\n    print(msg)\n\nasync def main():\n    task1 = asyncio.create_task(say_after(1, &#x27;Hello&#x27;))\n    task2 = asyncio.create_task(say_after(2, &#x27;World&#x27;))\n    await task1\n    await task2\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>create_task<\/code> \u4f1a\u7acb\u5373\u5c06\u534f\u7a0b\u6ce8\u518c\u5230\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f46\u53ea\u6709\u4e8b\u4ef6\u5faa\u73af\u8fd0\u884c\u65f6\u624d\u4f1a\u6267\u884c\u3002<\/p>\n<h2>3. Future \u5bf9\u8c61<\/h2>\n<p>Future \u662f\u5e95\u5c42\u5bf9\u8c61\uff0c\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u7ed3\u679c\u3002Task \u662f Future \u7684\u5b50\u7c7b\u3002\u901a\u5e38\u6211\u4eec\u4e0d\u9700\u8981\u76f4\u63a5\u64cd\u4f5c Future\uff0c\u4f46\u7406\u89e3\u5b83\u6709\u52a9\u4e8e\u6df1\u5165\u7406\u89e3 asyncio\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def set_after(fut, delay, value):\n    await asyncio.sleep(delay)\n    fut.set_result(value)\n\nasync def main():\n    loop = asyncio.get_running_loop()\n    fut = loop.create_future()\n    asyncio.create_task(set_after(fut, 1, &#x27;... done&#x27;))\n    result = await fut\n    print(result)\n\nasyncio.run(main())\n<\/code><\/pre>\n<h2>4. \u5e76\u53d1\u63a7\u5236\uff1agather \u4e0e wait<\/h2>\n<h3>4.1 asyncio.gather<\/h3>\n<p><code>gather<\/code> \u7528\u4e8e\u5e76\u53d1\u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\uff0c\u5e76\u7b49\u5f85\u6240\u6709\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def fetch_data(delay):\n    await asyncio.sleep(delay)\n    return f&#x27;Data after {delay}s&#x27;\n\nasync def main():\n    results = await asyncio.gather(\n        fetch_data(1),\n        fetch_data(2),\n        fetch_data(3)\n    )\n    print(results)\n\nasyncio.run(main())\n<\/code><\/pre>\n<h3>4.2 asyncio.wait<\/h3>\n<p><code>wait<\/code> \u63d0\u4f9b\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u7b49\u5f85\u7b2c\u4e00\u4e2a\u5b8c\u6210\u3001\u6240\u6709\u5b8c\u6210\u7b49\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def main():\n    tasks = [asyncio.create_task(fetch_data(i)) for i in [1, 2, 3]]\n    done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)\n    for task in done:\n        print(task.result())\n    for task in pending:\n        task.cancel()\n\nasyncio.run(main())\n<\/code><\/pre>\n<h2>5. \u907f\u514d\u5e38\u89c1\u9677\u9631<\/h2>\n<h3>5.1 \u4e0d\u8981\u5728\u534f\u7a0b\u4e2d\u4f7f\u7528 time.sleep<\/h3>\n<p><code>time.sleep<\/code> \u4f1a\u963b\u585e\u6574\u4e2a\u7ebf\u7a0b\uff0c\u5e94\u4f7f\u7528 <code>asyncio.sleep<\/code>\u3002<\/p>\n<pre><code class=\"language-python\">\n# \u9519\u8bef\nasync def bad():\n    import time\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>5.2 \u907f\u514d\u534f\u7a0b\u4e2d\u6267\u884c CPU \u5bc6\u96c6\u578b\u4efb\u52a1<\/h3>\n<p>CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\uff0c\u5e94\u4f7f\u7528 <code>asyncio.to_thread<\/code> \u6216 <code>concurrent.futures<\/code> \u5c06\u5176\u8f6c\u79fb\u5230\u7ebf\u7a0b\u6c60\u3002<\/p>\n<pre><code class=\"language-python\">\nimport asyncio\n\ndef cpu_bound():\n    count = 0\n    for i in range(10**7):\n        count += i\n    return count\n\nasync def main():\n    result = await asyncio.to_thread(cpu_bound)\n    print(result)\n\nasyncio.run(main())\n<\/code><\/pre>\n<h3>5.3 \u6b63\u786e\u4f7f\u7528\u9501<\/h3>\n<p>asyncio \u63d0\u4f9b\u4e86\u534f\u7a0b\u5b89\u5168\u7684\u9501 <code>asyncio.Lock<\/code>\u3002<\/p>\n<pre><code class=\"language-python\">\nasync def worker(lock, name):\n    async with lock:\n        print(f&#x27;{name} acquired lock&#x27;)\n        await asyncio.sleep(1)\n        print(f&#x27;{name} released lock&#x27;)\n\nasync def main():\n    lock = asyncio.Lock()\n    await asyncio.gather(\n        worker(lock, &#x27;A&#x27;),\n        worker(lock, &#x27;B&#x27;)\n    )\n\nasyncio.run(main())\n<\/code><\/pre>\n<h2>6. \u5b9e\u6218\uff1a\u5f02\u6b65 Web \u722c\u866b<\/h2>\n<p>\u7ed3\u5408 <code>aiohttp<\/code> \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65\u722c\u866b\u3002<\/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:\/\/example.com&#x27;,\n        &#x27;https:\/\/httpbin.org\/get&#x27;,\n        &#x27;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#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(len(result))\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u4f7f\u7528 <code>aiohttp<\/code> \u65f6\uff0c\u9700\u8981\u5b89\u88c5 <code>aiohttp<\/code> \u5e93\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u6df1\u5165\u4ecb\u7ecd\u4e86 asyncio \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5305\u62ec\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u548c Future\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u7f16\u5199\u9ad8\u6548\u7684\u5f02\u6b65\u4ee3\u7801\u3002\u8bb0\u4f4f\uff1a\u907f\u514d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\uff0c\u5408\u7406\u4f7f\u7528\u5e76\u53d1\u63a7\u5236\uff0c\u6b63\u786e\u5904\u7406 IO \u548c CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\">Python asyncio \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/docs.aiohttp.org\/\">aiohttp \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u5256\u6790 Python asyncio \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5305\u62ec\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u548c Future\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5c55\u793a\u5982\u4f55\u7f16\u5199\u9ad8\u6548\u7684\u5f02\u6b65\u4ee3\u7801\uff0c\u907f\u514d\u5e38\u89c1\u9677\u9631\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,104,105,103],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-3","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\/196","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=196"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}