{"id":10021231,"date":"2026-02-01T02:40:32","date_gmt":"2026-01-31T18:40:32","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021231"},"modified":"2026-02-01T02:40:32","modified_gmt":"2026-01-31T18:40:32","slug":"python%e4%b8%ad%e7%9a%84%e5%8d%8f%e7%a8%8b%e4%b8%8e%e4%ba%8b%e4%bb%b6%e5%be%aa%e7%8e%af%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021231","title":{"rendered":"Python\u4e2d\u7684\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u673a\u5236"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u673a\u5236<\/h2>\n<h3>1. \u534f\u7a0b\u7684\u6982\u5ff5\u4e0e\u57fa\u672c\u539f\u7406<\/h3>\n<p>\u534f\u7a0b\uff08Coroutine\uff09\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u7ebf\u7a0b\u5185\u5b9e\u73b0\u5e76\u53d1\u64cd\u4f5c\u3002\u534f\u7a0b\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u6682\u505c\uff0c\u4fdd\u5b58\u5f53\u524d\u7684\u6267\u884c\u72b6\u6001\uff0c\u7136\u540e\u5728\u9002\u5f53\u7684\u65f6\u5019\u6062\u590d\u6267\u884c\u3002<\/p>\n<h4>1.1 \u534f\u7a0b\u7684\u5b9a\u4e49<\/h4>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c\u5e76\u5728\u7a0d\u540e\u6062\u590d\u7684\u51fd\u6570\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0c\u534f\u7a0b\u7684\u5207\u6362\u662f\u7531\u7a0b\u5e8f\u81ea\u8eab\u63a7\u5236\u7684\uff0c\u800c\u4e0d\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u534f\u7a0b\u7684\u5207\u6362\u5f00\u9500\u975e\u5e38\u5c0f\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u7684I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<h4>1.2 \u534f\u7a0b\u4e0e\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\u7684\u6bd4\u8f83<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u5e76\u53d1\u6a21\u578b<\/th>\n<th>\u4f18\u70b9<\/th>\n<th>\u7f3a\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7ebf\u7a0b<\/td>\n<td>\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\uff0c\u4f7f\u7528\u7b80\u5355<\/td>\n<td>\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u7ade\u6001\u6761\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u8fdb\u7a0b<\/td>\n<td>\u5b8c\u5168\u9694\u79bb\uff0c\u5b89\u5168\u6027\u9ad8<\/td>\n<td>\u5185\u5b58\u5360\u7528\u5927\uff0c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u590d\u6742<\/td>\n<\/tr>\n<tr>\n<td>\u534f\u7a0b<\/td>\n<td>\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u5c0f\uff0c\u5e76\u53d1\u5ea6\u9ad8<\/td>\n<td>\u9700\u8981\u663e\u5f0f yield \u63a7\u5236\u6743\uff0c\u7f16\u7a0b\u590d\u6742\u5ea6\u8f83\u9ad8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.3 \u534f\u7a0b\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<p>\u534f\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\u57fa\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u6982\u5ff5\uff1a<\/p>\n<ol>\n<li><strong>\u6682\u505c\u4e0e\u6062\u590d<\/strong>\uff1a\u534f\u7a0b\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c\uff0c\u4fdd\u5b58\u5f53\u524d\u7684\u6267\u884c\u72b6\u6001\uff0c\u7136\u540e\u5728\u9002\u5f53\u7684\u65f6\u5019\u6062\u590d\u6267\u884c<\/li>\n<li><strong>\u534f\u4f5c\u5f0f\u8c03\u5ea6<\/strong>\uff1a\u534f\u7a0b\u7684\u5207\u6362\u662f\u7531\u7a0b\u5e8f\u81ea\u8eab\u63a7\u5236\u7684\uff0c\u800c\u4e0d\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u7684<\/li>\n<li><strong>\u4e8b\u4ef6\u5faa\u73af<\/strong>\uff1a\u534f\u7a0b\u9700\u8981\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u8fd0\u884c\uff0c\u4e8b\u4ef6\u5faa\u73af\u8d1f\u8d23\u8c03\u5ea6\u548c\u6267\u884c\u534f\u7a0b\u4efb\u52a1<\/li>\n<\/ol>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u57fa\u672c\u539f\u7406\u793a\u4f8b\nimport time\n\n# \u7b80\u5355\u7684\u534f\u7a0b\u5b9e\u73b0\uff08\u4f7f\u7528\u751f\u6210\u5668\uff09\ndef simple_coroutine():\n    print(&quot;\u534f\u7a0b\u5f00\u59cb&quot;)\n    value = yield\n    print(f&quot;\u534f\u7a0b\u63a5\u6536\u5230\u503c\uff1a{value}&quot;)\n    value = yield &quot;\u534f\u7a0b\u8fd4\u56de\u503c&quot;\n    print(f&quot;\u534f\u7a0b\u63a5\u6536\u5230\u7b2c\u4e8c\u4e2a\u503c\uff1a{value}&quot;)\n    return &quot;\u534f\u7a0b\u7ed3\u675f&quot;\n\n# \u521b\u5efa\u534f\u7a0b\u5bf9\u8c61\ncoro = simple_coroutine()\n\n# \u542f\u52a8\u534f\u7a0b\nprint(&quot;\u542f\u52a8\u534f\u7a0b\uff1a&quot;)\nnext(coro)  # \u6267\u884c\u5230\u7b2c\u4e00\u4e2a yield\n\n# \u53d1\u9001\u503c\u5e76\u6062\u590d\u534f\u7a0b\nprint(&quot;\/n\u53d1\u9001\u7b2c\u4e00\u4e2a\u503c\uff1a&quot;)\ntry:\n    result = coro.send(&quot;Hello&quot;)  # \u53d1\u9001\u503c\u5e76\u6267\u884c\u5230\u7b2c\u4e8c\u4e2a yield\n    print(f&quot;\u534f\u7a0b\u8fd4\u56de\u503c\uff1a{result}&quot;)\n    \n    # \u53d1\u9001\u7b2c\u4e8c\u4e2a\u503c\n    print(&quot;\/n\u53d1\u9001\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot;)\n    result = coro.send(&quot;World&quot;)  # \u53d1\u9001\u503c\u5e76\u6267\u884c\u5230\u7ed3\u675f\nexcept StopIteration as e:\n    print(f&quot;\u534f\u7a0b\u7ed3\u675f\uff0c\u8fd4\u56de\u503c\uff1a{e.value}&quot;)\n\n# \u6d4b\u8bd5\u534f\u7a0b\u7684\u6682\u505c\u4e0e\u6062\u590d\nprint(&quot;\/n\u6d4b\u8bd5\u534f\u7a0b\u7684\u6682\u505c\u4e0e\u6062\u590d\uff1a&quot;)\n\ndef timer_coroutine():\n    &quot;&quot;&quot;\u8ba1\u65f6\u5668\u534f\u7a0b&quot;&quot;&quot;\n    start = time.time()\n    while True:\n        elapsed = time.time() - start\n        yield elapsed\n        time.sleep(0.5)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n\n# \u521b\u5efa\u8ba1\u65f6\u5668\u534f\u7a0b\n timer = timer_coroutine()\n\n# \u4f7f\u7528\u8ba1\u65f6\u5668\nprint(&quot;\u5f00\u59cb\u8ba1\u65f6\uff1a&quot;)\nfor i in range(5):\n    elapsed = next(timer)\n    print(f&quot;\u7b2c {i + 1} \u6b21\u8c03\u7528\uff0c\u5df2\u8fc7\u65f6\u95f4\uff1a{elapsed:.2f}\u79d2&quot;)<\/code><\/pre>\n<h4>1.4 \u534f\u7a0b\u7684\u4f18\u52bf<\/h4>\n<p>\u4f7f\u7528\u534f\u7a0b\u7684\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u5e76\u53d1<\/strong>\uff1a\u5355\u7ebf\u7a0b\u5185\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u534f\u7a0b\u4efb\u52a1<\/li>\n<li><strong>\u4f4e\u5f00\u9500<\/strong>\uff1a\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u975e\u5e38\u5c0f\uff0c\u4e0d\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u4ecb\u5165<\/li>\n<li><strong>\u65e0\u7ade\u6001\u6761\u4ef6<\/strong>\uff1a\u534f\u7a0b\u5728\u5355\u7ebf\u7a0b\u5185\u6267\u884c\uff0c\u4e0d\u9700\u8981\u9501\u673a\u5236<\/li>\n<li><strong>\u6613\u4e8e\u8c03\u8bd5<\/strong>\uff1a\u534f\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u662f\u786e\u5b9a\u7684\uff0c\u4fbf\u4e8e\u8c03\u8bd5<\/li>\n<li><strong>\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff1a\u534f\u7a0b\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u53ef\u4ee5\u6682\u505c\uff0c\u8ba9\u5176\u4ed6\u534f\u7a0b\u6267\u884c<\/li>\n<\/ul>\n<h3>2. Python\u4e2d\u7684\u534f\u7a0b\u5b9e\u73b0<\/h3>\n<p>Python\u4e2d\u7684\u534f\u7a0b\u5b9e\u73b0\u7ecf\u5386\u4e86\u51e0\u4e2a\u9636\u6bb5\u7684\u53d1\u5c55\uff1a<\/p>\n<ol>\n<li><strong>\u751f\u6210\u5668\u534f\u7a0b<\/strong>\uff1a\u57fa\u4e8e\u751f\u6210\u5668\u7684\u534f\u7a0b\u5b9e\u73b0\uff08Python 2.5+\uff09<\/li>\n<li><strong>\u589e\u5f3a\u578b\u751f\u6210\u5668\u534f\u7a0b<\/strong>\uff1a\u652f\u6301 <code>send()<\/code>\u3001<code>throw()<\/code> \u548c <code>close()<\/code> \u65b9\u6cd5\uff08Python 2.5+\uff09<\/li>\n<li><strong>\u539f\u751f\u534f\u7a0b<\/strong>\uff1a\u4f7f\u7528 <code>async\/await<\/code> \u8bed\u6cd5\u7684\u534f\u7a0b\uff08Python 3.5+\uff09<\/li>\n<\/ol>\n<h4>2.1 \u751f\u6210\u5668\u534f\u7a0b<\/h4>\n<p>\u751f\u6210\u5668\u534f\u7a0b\u662f\u57fa\u4e8ePython\u7684\u751f\u6210\u5668\u5b9e\u73b0\u7684\u534f\u7a0b\uff0c\u4f7f\u7528 <code>yield<\/code> \u8bed\u53e5\u6765\u6682\u505c\u6267\u884c\uff1a<\/p>\n<pre><code class=\"python\"># \u751f\u6210\u5668\u534f\u7a0b\u793a\u4f8b\ndef generator_coroutine():\n    &quot;&quot;&quot;\u751f\u6210\u5668\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u534f\u7a0b\u5f00\u59cb&quot;)\n    while True:\n        value = yield\n        print(f&quot;\u534f\u7a0b\u63a5\u6536\u5230\u503c\uff1a{value}&quot;)\n        if value == &quot;exit&quot;:\n            break\n    print(&quot;\u534f\u7a0b\u7ed3\u675f&quot;)\n\n# \u521b\u5efa\u534f\u7a0b\u5bf9\u8c61\ncoro = generator_coroutine()\n\n# \u542f\u52a8\u534f\u7a0b\nnext(coro)\n\n# \u53d1\u9001\u503c\ncoro.send(&quot;Hello&quot;)\ncoro.send(&quot;World&quot;)\ncoro.send(&quot;exit&quot;)\n\n# \u6d4b\u8bd5\u5e26\u8fd4\u56de\u503c\u7684\u751f\u6210\u5668\u534f\u7a0b\ndef counting_coroutine():\n    &quot;&quot;&quot;\u8ba1\u6570\u534f\u7a0b&quot;&quot;&quot;\n    count = 0\n    while True:\n        action = yield count\n        if action == &quot;increment&quot;:\n            count += 1\n        elif action == &quot;reset&quot;:\n            count = 0\n        elif action == &quot;exit&quot;:\n            break\n    return count\n\n# \u521b\u5efa\u534f\u7a0b\u5bf9\u8c61\ncoro = counting_coroutine()\n\n# \u542f\u52a8\u534f\u7a0b\nprint(f&quot;\u521d\u59cb\u503c\uff1a{next(coro)}&quot;)\n\n# \u53d1\u9001\u64cd\u4f5c\nprint(f&quot;\u9012\u589e\u540e\uff1a{coro.send('increment')}&quot;)\nprint(f&quot;\u9012\u589e\u540e\uff1a{coro.send('increment')}&quot;)\nprint(f&quot;\u91cd\u7f6e\u540e\uff1a{coro.send('reset')}&quot;)\nprint(f&quot;\u9012\u589e\u540e\uff1a{coro.send('increment')}&quot;)\n\n# \u9000\u51fa\u534f\u7a0b\ntry:\n    coro.send(&quot;exit&quot;)\nexcept StopIteration as e:\n    print(f&quot;\u534f\u7a0b\u7ed3\u675f\uff0c\u6700\u7ec8\u8ba1\u6570\uff1a{e.value}&quot;)<\/code><\/pre>\n<h4>2.2 \u539f\u751f\u534f\u7a0b<\/h4>\n<p>\u539f\u751f\u534f\u7a0b\u662fPython 3.5+\u5f15\u5165\u7684\u534f\u7a0b\u5b9e\u73b0\uff0c\u4f7f\u7528 <code>async\/await<\/code> \u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"python\"># \u539f\u751f\u534f\u7a0b\u793a\u4f8b\nimport asyncio\n\nasync def native_coroutine():\n    &quot;&quot;&quot;\u539f\u751f\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u534f\u7a0b\u5f00\u59cb&quot;)\n    await asyncio.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u534f\u7a0b\u7ee7\u7eed&quot;)\n    await asyncio.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u534f\u7a0b\u7ed3\u675f&quot;)\n    return &quot;\u534f\u7a0b\u8fd4\u56de\u503c&quot;\n\n# \u8fd0\u884c\u534f\u7a0b\nasync def main():\n    result = await native_coroutine()\n    print(f&quot;\u534f\u7a0b\u8fd4\u56de\u503c\uff1a{result}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\uff1a&quot;)\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u5e26\u53c2\u6570\u7684\u539f\u751f\u534f\u7a0b\nasync def greet(name):\n    &quot;&quot;&quot;\u95ee\u5019\u534f\u7a0b&quot;&quot;&quot;\n    print(f&quot;Hello, {name}!&quot;)\n    await asyncio.sleep(1)\n    print(f&quot;Goodbye, {name}!&quot;)\n    return f&quot;Greeted {name}&quot;\n\n# \u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\nasync def main_multiple():\n    # \u5e76\u53d1\u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\n    task1 = asyncio.create_task(greet(&quot;Alice&quot;))\n    task2 = asyncio.create_task(greet(&quot;Bob&quot;))\n    task3 = asyncio.create_task(greet(&quot;Charlie&quot;))\n    \n    # \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n    results = await asyncio.gather(task1, task2, task3)\n    print(f&quot;\u6240\u6709\u534f\u7a0b\u5b8c\u6210\uff0c\u7ed3\u679c\uff1a{results}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\/n\u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\uff1a&quot;)\nasyncio.run(main_multiple())<\/code><\/pre>\n<h4>2.3 \u534f\u7a0b\u88c5\u9970\u5668<\/h4>\n<p>\u5728Python 3.4\u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c\u9700\u8981\u4f7f\u7528 <code>@asyncio.coroutine<\/code> \u88c5\u9970\u5668\u6765\u6807\u8bb0\u534f\u7a0b\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u88c5\u9970\u5668\u793a\u4f8b\nimport asyncio\n\n@asyncio.coroutine\ndef decorated_coroutine():\n    &quot;&quot;&quot;\u4f7f\u7528\u88c5\u9970\u5668\u7684\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u534f\u7a0b\u5f00\u59cb&quot;)\n    yield from asyncio.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u534f\u7a0b\u7ee7\u7eed&quot;)\n    yield from asyncio.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u534f\u7a0b\u7ed3\u675f&quot;)\n    return &quot;\u534f\u7a0b\u8fd4\u56de\u503c&quot;\n\n# \u8fd0\u884c\u534f\u7a0b\n@asyncio.coroutine\ndef main():\n    result = yield from decorated_coroutine()\n    print(f&quot;\u534f\u7a0b\u8fd4\u56de\u503c\uff1a{result}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\uff1a&quot;)\nasyncio.run(main())<\/code><\/pre>\n<h3>3. \u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f\u534f\u7a0b\u6267\u884c\u7684\u6838\u5fc3\uff0c\u5b83\u8d1f\u8d23\u8c03\u5ea6\u548c\u6267\u884c\u534f\u7a0b\u4efb\u52a1\uff0c\u5904\u7406I\/O\u64cd\u4f5c\u7b49\u3002Python\u7684 <code>asyncio<\/code> \u5e93\u63d0\u4f9b\u4e86\u4e8b\u4ef6\u5faa\u73af\u7684\u5b9e\u73b0\u3002<\/p>\n<h4>3.1 \u4e8b\u4ef6\u5faa\u73af\u7684\u6982\u5ff5<\/h4>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5b83\u4e0d\u65ad\u5730\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\uff0c\u76f4\u5230\u6240\u6709\u4efb\u52a1\u90fd\u5b8c\u6210\u3002\u4e8b\u4ef6\u5faa\u73af\u7684\u4e3b\u8981\u804c\u8d23\u5305\u62ec\uff1a<\/p>\n<ol>\n<li><strong>\u4efb\u52a1\u8c03\u5ea6<\/strong>\uff1a\u8c03\u5ea6\u548c\u6267\u884c\u534f\u7a0b\u4efb\u52a1<\/li>\n<li><strong>I\/O\u64cd\u4f5c\u5904\u7406<\/strong>\uff1a\u5904\u7406\u5f02\u6b65I\/O\u64cd\u4f5c<\/li>\n<li><strong>\u4e8b\u4ef6\u5904\u7406<\/strong>\uff1a\u5904\u7406\u5b9a\u65f6\u5668\u3001\u4fe1\u53f7\u7b49\u4e8b\u4ef6<\/li>\n<li><strong>\u56de\u8c03\u51fd\u6570\u6267\u884c<\/strong>\uff1a\u6267\u884c\u6ce8\u518c\u7684\u56de\u8c03\u51fd\u6570<\/li>\n<\/ol>\n<h4>3.2 \u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u6d41\u7a0b<\/h4>\n<p>\u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316<\/strong>\uff1a\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u5bf9\u8c61<\/li>\n<li><strong>\u6dfb\u52a0\u4efb\u52a1<\/strong>\uff1a\u5c06\u534f\u7a0b\u4efb\u52a1\u6dfb\u52a0\u5230\u4e8b\u4ef6\u5faa\u73af\u4e2d<\/li>\n<li><strong>\u6267\u884c\u4efb\u52a1<\/strong>\uff1a\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c<\/li>\n<li><strong>\u5904\u7406I\/O<\/strong>\uff1a\u5f53\u4efb\u52a1\u9700\u8981\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\uff0c\u6682\u505c\u4efb\u52a1\u6267\u884c\uff0c\u5904\u7406\u5176\u4ed6\u4efb\u52a1<\/li>\n<li><strong>\u4efb\u52a1\u5b8c\u6210<\/strong>\uff1a\u5f53I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u6062\u590d\u6682\u505c\u7684\u4efb\u52a1\u6267\u884c<\/li>\n<li><strong>\u5faa\u73af\u7ed3\u675f<\/strong>\uff1a\u5f53\u6240\u6709\u4efb\u52a1\u90fd\u5b8c\u6210\u65f6\uff0c\u9000\u51fa\u4e8b\u4ef6\u5faa\u73af<\/li>\n<\/ol>\n<pre><code class=\"python\"># \u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u539f\u7406\u793a\u4f8b\nimport asyncio\nimport time\n\nasync def task1():\n    &quot;&quot;&quot;\u4efb\u52a11&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a11\u5f00\u59cb&quot;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u4efb\u52a11\u7ed3\u675f&quot;)\n    return &quot;\u4efb\u52a11\u8fd4\u56de\u503c&quot;\n\nasync def task2():\n    &quot;&quot;&quot;\u4efb\u52a12&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a12\u5f00\u59cb&quot;)\n    await asyncio.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u4efb\u52a12\u7ed3\u675f&quot;)\n    return &quot;\u4efb\u52a12\u8fd4\u56de\u503c&quot;\n\nasync def task3():\n    &quot;&quot;&quot;\u4efb\u52a13&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a13\u5f00\u59cb&quot;)\n    await asyncio.sleep(1.5)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u4efb\u52a13\u7ed3\u675f&quot;)\n    return &quot;\u4efb\u52a13\u8fd4\u56de\u503c&quot;\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(f&quot;\u4e3b\u534f\u7a0b\u5f00\u59cb\uff0c\u65f6\u95f4\uff1a{time.strftime('%H:%M:%S')}&quot;)\n    \n    # \u521b\u5efa\u4efb\u52a1\n    task1_obj = asyncio.create_task(task1())\n    task2_obj = asyncio.create_task(task2())\n    task3_obj = asyncio.create_task(task3())\n    \n    # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n    results = await asyncio.gather(task1_obj, task2_obj, task3_obj)\n    \n    print(f&quot;\u4e3b\u534f\u7a0b\u7ed3\u675f\uff0c\u65f6\u95f4\uff1a{time.strftime('%H:%M:%S')}&quot;)\n    print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{results}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\uff1a&quot;)\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u4e8b\u4ef6\u5faa\u73af\u7684\u4efb\u52a1\u8c03\u5ea6\nasync def periodic_task(name, interval):\n    &quot;&quot;&quot;\u5468\u671f\u6027\u4efb\u52a1&quot;&quot;&quot;\n    for i in range(3):\n        print(f&quot;{name} \u6267\u884c\uff0c\u7b2c {i + 1} \u6b21\uff0c\u65f6\u95f4\uff1a{time.strftime('%H:%M:%S')}&quot;)\n        await asyncio.sleep(interval)\n\nasync def main_periodic():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(f&quot;\u4e3b\u534f\u7a0b\u5f00\u59cb\uff0c\u65f6\u95f4\uff1a{time.strftime('%H:%M:%S')}&quot;)\n    \n    # \u521b\u5efa\u5468\u671f\u6027\u4efb\u52a1\n    task1 = asyncio.create_task(periodic_task(&quot;\u4efb\u52a1A&quot;, 1))\n    task2 = asyncio.create_task(periodic_task(&quot;\u4efb\u52a1B&quot;, 2))\n    \n    # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n    await asyncio.gather(task1, task2)\n    \n    print(f&quot;\u4e3b\u534f\u7a0b\u7ed3\u675f\uff0c\u65f6\u95f4\uff1a{time.strftime('%H:%M:%S')}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\/n\u6d4b\u8bd5\u5468\u671f\u6027\u4efb\u52a1\uff1a&quot;)\nasyncio.run(main_periodic())<\/code><\/pre>\n<h4>3.3 \u4e8b\u4ef6\u5faa\u73af\u7684\u7c7b\u578b<\/h4>\n<p>Python\u7684 <code>asyncio<\/code> \u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u4ef6\u5faa\u73af\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e73\u53f0\u548c\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong>SelectorEventLoop<\/strong>\uff1a\u57fa\u4e8e select \u7cfb\u7edf\u8c03\u7528\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u9002\u7528\u4e8e\u6240\u6709\u5e73\u53f0<\/li>\n<li><strong>ProactorEventLoop<\/strong>\uff1a\u57fa\u4e8e IOCP \u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ec5\u9002\u7528\u4e8e Windows \u5e73\u53f0<\/li>\n<li><strong>uvloop<\/strong>\uff1a\u57fa\u4e8e libuv \u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u6027\u80fd\u66f4\u9ad8\uff0c\u4f46\u9700\u8981\u5355\u72ec\u5b89\u88c5<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u4e8b\u4ef6\u5faa\u73af\u7684\u7c7b\u578b\u793a\u4f8b\nimport asyncio\n\n# \u83b7\u53d6\u5f53\u524d\u4e8b\u4ef6\u5faa\u73af\nloop = asyncio.get_event_loop()\nprint(f&quot;\u5f53\u524d\u4e8b\u4ef6\u5faa\u73af\uff1a{type(loop).__name__}&quot;)\n\n# \u6d4b\u8bd5\u4e0d\u540c\u7684\u4e8b\u4ef6\u5faa\u73af\u7b56\u7565\nprint(&quot;\/n\u6d4b\u8bd5\u4e8b\u4ef6\u5faa\u73af\u7b56\u7565\uff1a&quot;)\n\n# \u9ed8\u8ba4\u7b56\u7565\ndefault_policy = asyncio.get_event_loop_policy()\nprint(f&quot;\u9ed8\u8ba4\u7b56\u7565\uff1a{type(default_policy).__name__}&quot;)\n\n# \u5c1d\u8bd5\u4f7f\u7528 uvloop\nprint(&quot;\/n\u5c1d\u8bd5\u4f7f\u7528 uvloop\uff1a&quot;)\ntry:\n    import uvloop\n    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())\n    loop = asyncio.new_event_loop()\n    print(f&quot;uvloop \u4e8b\u4ef6\u5faa\u73af\uff1a{type(loop).__name__}&quot;)\nexcept ImportError:\n    print(&quot;uvloop \u672a\u5b89\u88c5&quot;)\n\n# \u6d4b\u8bd5\u4e8b\u4ef6\u5faa\u73af\u7684\u5173\u95ed\nprint(&quot;\/n\u6d4b\u8bd5\u4e8b\u4ef6\u5faa\u73af\u7684\u5173\u95ed\uff1a&quot;)\n\nasync def test_task():\n    print(&quot;\u6d4b\u8bd5\u4efb\u52a1&quot;)\n    await asyncio.sleep(1)\n    print(&quot;\u6d4b\u8bd5\u4efb\u52a1\u5b8c\u6210&quot;)\n\n# \u521b\u5efa\u5e76\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\nloop = asyncio.new_event_loop()\ntry:\n    loop.run_until_complete(test_task())\nfinally:\n    loop.close()\n    print(&quot;\u4e8b\u4ef6\u5faa\u73af\u5df2\u5173\u95ed&quot;)<\/code><\/pre>\n<h3>4. \u534f\u7a0b\u7684\u9ad8\u7ea7\u7279\u6027<\/h3>\n<h4>4.1 \u534f\u7a0b\u7684\u53d6\u6d88<\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>cancel()<\/code> \u65b9\u6cd5\u6765\u53d6\u6d88\u6b63\u5728\u6267\u884c\u7684\u534f\u7a0b\u4efb\u52a1\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u53d6\u6d88\u793a\u4f8b\nimport asyncio\nimport time\n\nasync def long_running_task():\n    &quot;&quot;&quot;\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1&quot;&quot;&quot;\n    print(&quot;\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u5f00\u59cb&quot;)\n    try:\n        for i in range(10):\n            print(f&quot;\u4efb\u52a1\u6267\u884c\u4e2d... {i + 1}\/10&quot;)\n            await asyncio.sleep(1)\n    except asyncio.CancelledError:\n        print(&quot;\u4efb\u52a1\u88ab\u53d6\u6d88&quot;)\n        raise  # \u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff0c\u786e\u4fdd\u4efb\u52a1\u6b63\u786e\u7ed3\u675f\n    finally:\n        print(&quot;\u4efb\u52a1\u6e05\u7406&quot;)\n    return &quot;\u4efb\u52a1\u5b8c\u6210&quot;\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    # \u521b\u5efa\u4efb\u52a1\n    task = asyncio.create_task(long_running_task())\n    \n    # \u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u53d6\u6d88\u4efb\u52a1\n    await asyncio.sleep(3)\n    print(&quot;\u53d6\u6d88\u4efb\u52a1&quot;)\n    task.cancel()\n    \n    # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n    try:\n        result = await task\n        print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{result}&quot;)\n    except asyncio.CancelledError:\n        print(&quot;\u6355\u83b7\u5230\u4efb\u52a1\u53d6\u6d88\u5f02\u5e38&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u6d4b\u8bd5\u534f\u7a0b\u7684\u53d6\u6d88\uff1a&quot;)\nasyncio.run(main())<\/code><\/pre>\n<h4>4.2 \u534f\u7a0b\u7684\u8d85\u65f6\u5904\u7406<\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>asyncio.wait_for()<\/code> \u51fd\u6570\u6765\u8bbe\u7f6e\u534f\u7a0b\u7684\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u8d85\u65f6\u5904\u7406\u793a\u4f8b\nimport asyncio\n\nasync def slow_task():\n    &quot;&quot;&quot;\u6162\u901f\u4efb\u52a1&quot;&quot;&quot;\n    print(&quot;\u6162\u901f\u4efb\u52a1\u5f00\u59cb&quot;)\n    await asyncio.sleep(5)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    print(&quot;\u6162\u901f\u4efb\u52a1\u7ed3\u675f&quot;)\n    return &quot;\u6162\u901f\u4efb\u52a1\u8fd4\u56de\u503c&quot;\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u6d4b\u8bd5\u8d85\u65f6\u5904\u7406\uff1a&quot;)\n    \n    try:\n        # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a3\u79d2\n        result = await asyncio.wait_for(slow_task(), timeout=3)\n        print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{result}&quot;)\n    except asyncio.TimeoutError:\n        print(&quot;\u4efb\u52a1\u8d85\u65f6&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u5e26\u8d85\u65f6\u7684\u5e76\u884c\u4efb\u52a1\nasync def task_with_timeout(name, delay):\n    &quot;&quot;&quot;\u5e26\u5ef6\u8fdf\u7684\u4efb\u52a1&quot;&quot;&quot;\n    print(f&quot;\u4efb\u52a1 {name} \u5f00\u59cb\uff0c\u5ef6\u8fdf {delay} \u79d2&quot;)\n    await asyncio.sleep(delay)\n    print(f&quot;\u4efb\u52a1 {name} \u7ed3\u675f&quot;)\n    return f&quot;\u4efb\u52a1 {name} \u8fd4\u56de\u503c&quot;\n\nasync def main_multiple():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\/n\u6d4b\u8bd5\u5e26\u8d85\u65f6\u7684\u5e76\u884c\u4efb\u52a1\uff1a&quot;)\n    \n    try:\n        # \u521b\u5efa\u4efb\u52a1\n        task1 = task_with_timeout(&quot;A&quot;, 2)\n        task2 = task_with_timeout(&quot;B&quot;, 4)\n        task3 = task_with_timeout(&quot;C&quot;, 1)\n        \n        # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a3\u79d2\n        results = await asyncio.wait_for(\n            asyncio.gather(task1, task2, task3),\n            timeout=3\n        )\n        print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{results}&quot;)\n    except asyncio.TimeoutError:\n        print(&quot;\u4efb\u52a1\u8d85\u65f6&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main_multiple())<\/code><\/pre>\n<h4>4.3 \u534f\u7a0b\u7684\u5f02\u5e38\u5904\u7406<\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 try-except \u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u534f\u7a0b\u4e2d\u7684\u5f02\u5e38\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u5f02\u5e38\u5904\u7406\u793a\u4f8b\nimport asyncio\n\nasync def task_with_exception():\n    &quot;&quot;&quot;\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u4efb\u52a1&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a1\u5f00\u59cb&quot;)\n    await asyncio.sleep(1)\n    raise ValueError(&quot;\u4efb\u52a1\u6267\u884c\u51fa\u9519&quot;)\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u6d4b\u8bd5\u5f02\u5e38\u5904\u7406\uff1a&quot;)\n    \n    try:\n        result = await task_with_exception()\n        print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{result}&quot;)\n    except ValueError as e:\n        print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u5e76\u884c\u4efb\u52a1\u7684\u5f02\u5e38\u5904\u7406\nasync def task1():\n    &quot;&quot;&quot;\u4efb\u52a11&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a11\u5f00\u59cb&quot;)\n    await asyncio.sleep(1)\n    raise ValueError(&quot;\u4efb\u52a11\u51fa\u9519&quot;)\n\nasync def task2():\n    &quot;&quot;&quot;\u4efb\u52a12&quot;&quot;&quot;\n    print(&quot;\u4efb\u52a12\u5f00\u59cb&quot;)\n    await asyncio.sleep(2)\n    print(&quot;\u4efb\u52a12\u7ed3\u675f&quot;)\n    return &quot;\u4efb\u52a12\u8fd4\u56de\u503c&quot;\n\nasync def main_multiple():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\/n\u6d4b\u8bd5\u5e76\u884c\u4efb\u52a1\u7684\u5f02\u5e38\u5904\u7406\uff1a&quot;)\n    \n    try:\n        # \u521b\u5efa\u4efb\u52a1\n        task1_obj = asyncio.create_task(task1())\n        task2_obj = asyncio.create_task(task2())\n        \n        # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n        results = await asyncio.gather(task1_obj, task2_obj)\n        print(f&quot;\u4efb\u52a1\u7ed3\u679c\uff1a{results}&quot;)\n    except ValueError as e:\n        print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main_multiple())<\/code><\/pre>\n<h4>4.4 \u534f\u7a0b\u7684\u5d4c\u5957<\/h4>\n<p>\u534f\u7a0b\u53ef\u4ee5\u5d4c\u5957\u8c03\u7528\uff0c\u5f62\u6210\u534f\u7a0b\u94fe\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u5d4c\u5957\u793a\u4f8b\nimport asyncio\n\nasync def inner_coroutine():\n    &quot;&quot;&quot;\u5185\u90e8\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u5185\u90e8\u534f\u7a0b\u5f00\u59cb&quot;)\n    await asyncio.sleep(1)\n    print(&quot;\u5185\u90e8\u534f\u7a0b\u7ed3\u675f&quot;)\n    return &quot;\u5185\u90e8\u534f\u7a0b\u8fd4\u56de\u503c&quot;\n\nasync def middle_coroutine():\n    &quot;&quot;&quot;\u4e2d\u95f4\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u4e2d\u95f4\u534f\u7a0b\u5f00\u59cb&quot;)\n    result = await inner_coroutine()\n    print(f&quot;\u83b7\u53d6\u5185\u90e8\u534f\u7a0b\u7ed3\u679c\uff1a{result}&quot;)\n    await asyncio.sleep(1)\n    print(&quot;\u4e2d\u95f4\u534f\u7a0b\u7ed3\u675f&quot;)\n    return f&quot;\u4e2d\u95f4\u534f\u7a0b\u8fd4\u56de\u503c\uff0c\u5185\u90e8\u7ed3\u679c\uff1a{result}&quot;\n\nasync def outer_coroutine():\n    &quot;&quot;&quot;\u5916\u90e8\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u5916\u90e8\u534f\u7a0b\u5f00\u59cb&quot;)\n    result = await middle_coroutine()\n    print(f&quot;\u83b7\u53d6\u4e2d\u95f4\u534f\u7a0b\u7ed3\u679c\uff1a{result}&quot;)\n    await asyncio.sleep(1)\n    print(&quot;\u5916\u90e8\u534f\u7a0b\u7ed3\u675f&quot;)\n    return f&quot;\u5916\u90e8\u534f\u7a0b\u8fd4\u56de\u503c\uff0c\u4e2d\u95f4\u7ed3\u679c\uff1a{result}&quot;\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\u6d4b\u8bd5\u534f\u7a0b\u5d4c\u5957\uff1a&quot;)\n    result = await outer_coroutine()\n    print(f&quot;\u6700\u7ec8\u7ed3\u679c\uff1a{result}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u6df1\u5ea6\u5d4c\u5957\nasync def nested_coroutine(depth):\n    &quot;&quot;&quot;\u6df1\u5ea6\u5d4c\u5957\u7684\u534f\u7a0b&quot;&quot;&quot;\n    if depth &gt; 0:\n        print(f&quot;\u5d4c\u5957\u6df1\u5ea6 {depth} \u5f00\u59cb&quot;)\n        result = await nested_coroutine(depth - 1)\n        print(f&quot;\u5d4c\u5957\u6df1\u5ea6 {depth} \u7ed3\u675f\uff0c\u83b7\u53d6\u7ed3\u679c\uff1a{result}&quot;)\n        return f&quot;\u6df1\u5ea6 {depth} \u8fd4\u56de\u503c&quot;\n    else:\n        print(&quot;\u5d4c\u5957\u6df1\u5ea6 0 \u5f00\u59cb&quot;)\n        await asyncio.sleep(0.5)\n        print(&quot;\u5d4c\u5957\u6df1\u5ea6 0 \u7ed3\u675f&quot;)\n        return &quot;\u6df1\u5ea6 0 \u8fd4\u56de\u503c&quot;\n\nasync def main_depth():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    print(&quot;\/n\u6d4b\u8bd5\u6df1\u5ea6\u5d4c\u5957\uff1a&quot;)\n    result = await nested_coroutine(5)\n    print(f&quot;\u6700\u7ec8\u7ed3\u679c\uff1a{result}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main_depth())<\/code><\/pre>\n<h3>5. \u534f\u7a0b\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<h4>5.1 \u7f51\u7edc\u7f16\u7a0b<\/h4>\n<p>\u534f\u7a0b\u975e\u5e38\u9002\u5408\u7f51\u7edc\u7f16\u7a0b\uff0c\u7279\u522b\u662f\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\nimport asyncio\nimport aiohttp\n\nasync def fetch_url(session, url):\n    &quot;&quot;&quot;\u83b7\u53d6URL\u5185\u5bb9&quot;&quot;&quot;\n    try:\n        async with session.get(url) as response:\n            status = response.status\n            content_length = response.content_length or 0\n            print(f&quot;URL: {url}, \u72b6\u6001\u7801: {status}, \u5185\u5bb9\u957f\u5ea6: {content_length}&quot;)\n            # \u8bfb\u53d6\u54cd\u5e94\u5185\u5bb9\n            await response.read()\n            return status\n    except Exception as e:\n        print(f&quot;URL: {url}, \u9519\u8bef: {e}&quot;)\n        return None\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    urls = [\n        &quot;https:\/\/www.example.com&quot;,\n        &quot;https:\/\/www.google.com&quot;,\n        &quot;https:\/\/www.github.com&quot;,\n        &quot;https:\/\/www.python.org&quot;,\n        &quot;https:\/\/www.baidu.com&quot;,\n        &quot;https:\/\/www.microsoft.com&quot;,\n        &quot;https:\/\/www.apple.com&quot;,\n        &quot;https:\/\/www.amazon.com&quot;,\n        &quot;https:\/\/www.facebook.com&quot;,\n        &quot;https:\/\/www.twitter.com&quot;\n    ]\n    \n    print(f&quot;\u5f00\u59cb\u83b7\u53d6 {len(urls)} \u4e2aURL&quot;)\n    \n    # \u521b\u5efa\u4f1a\u8bdd\n    async with aiohttp.ClientSession() as session:\n        # \u521b\u5efa\u4efb\u52a1\n        tasks = [fetch_url(session, url) for url in urls]\n        # \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n        results = await asyncio.gather(*tasks)\n    \n    print(f&quot;\/n\u6240\u6709URL\u83b7\u53d6\u5b8c\u6210\uff0c\u6210\u529f: {results.count(200)}, \u5931\u8d25: {results.count(None)}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u6d4b\u8bd5\u534f\u7a0b\u7f51\u7edc\u7f16\u7a0b\uff1a&quot;)\nasyncio.run(main())<\/code><\/pre>\n<h4>5.2 \u5e76\u53d1\u4efb\u52a1\u5904\u7406<\/h4>\n<p>\u534f\u7a0b\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u4efb\u52a1\uff0c\u5982\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u64cd\u4f5c\u7b49\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u5728\u5e76\u53d1\u4efb\u52a1\u5904\u7406\u4e2d\u7684\u5e94\u7528\nimport asyncio\nimport time\n\nasync def process_task(task_id, delay):\n    &quot;&quot;&quot;\u5904\u7406\u4efb\u52a1&quot;&quot;&quot;\n    print(f&quot;\u4efb\u52a1 {task_id} \u5f00\u59cb\uff0c\u5ef6\u8fdf {delay} \u79d2&quot;)\n    await asyncio.sleep(delay)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    result = task_id * 10\n    print(f&quot;\u4efb\u52a1 {task_id} \u7ed3\u675f\uff0c\u7ed3\u679c: {result}&quot;)\n    return result\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    # \u521b\u5efa\u4efb\u52a1\u5217\u8868\n    tasks = [\n        process_task(1, 2),\n        process_task(2, 1),\n        process_task(3, 3),\n        process_task(4, 1.5),\n        process_task(5, 2.5),\n        process_task(6, 0.5),\n        process_task(7, 1.2),\n        process_task(8, 2.8),\n        process_task(9, 0.8),\n        process_task(10, 1.8)\n    ]\n    \n    print(f&quot;\u5f00\u59cb\u5904\u7406 {len(tasks)} \u4e2a\u4efb\u52a1&quot;)\n    start_time = time.time()\n    \n    # \u5e76\u53d1\u5904\u7406\u6240\u6709\u4efb\u52a1\n    results = await asyncio.gather(*tasks)\n    \n    end_time = time.time()\n    print(f&quot;\/n\u6240\u6709\u4efb\u52a1\u5904\u7406\u5b8c\u6210\uff0c\u8017\u65f6: {end_time - start_time:.2f}\u79d2&quot;)\n    print(f&quot;\u4efb\u52a1\u7ed3\u679c: {results}&quot;)\n    print(f&quot;\u7ed3\u679c\u603b\u548c: {sum(results)}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u6d4b\u8bd5\u534f\u7a0b\u5e76\u53d1\u4efb\u52a1\u5904\u7406\uff1a&quot;)\nasyncio.run(main())\n\n# \u6d4b\u8bd5\u6279\u91cf\u4efb\u52a1\u5904\u7406\nasync def batch_process(tasks, batch_size=5):\n    &quot;&quot;&quot;\u6279\u91cf\u5904\u7406\u4efb\u52a1&quot;&quot;&quot;\n    results = []\n    \n    for i in range(0, len(tasks), batch_size):\n        batch = tasks[i:i + batch_size]\n        print(f&quot;\u5904\u7406\u6279\u6b21 {i\/\/batch_size + 1}, \u4efb\u52a1\u6570\u91cf: {len(batch)}&quot;)\n        batch_results = await asyncio.gather(*batch)\n        results.extend(batch_results)\n    \n    return results\n\nasync def main_batch():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    # \u521b\u5efa\u5927\u91cf\u4efb\u52a1\n    tasks = [process_task(i, 0.1) for i in range(1, 21)]\n    \n    print(f&quot;\/n\u5f00\u59cb\u6279\u91cf\u5904\u7406 {len(tasks)} \u4e2a\u4efb\u52a1&quot;)\n    start_time = time.time()\n    \n    # \u6279\u91cf\u5904\u7406\u4efb\u52a1\n    results = await batch_process(tasks, batch_size=5)\n    \n    end_time = time.time()\n    print(f&quot;\/n\u6240\u6709\u4efb\u52a1\u5904\u7406\u5b8c\u6210\uff0c\u8017\u65f6: {end_time - start_time:.2f}\u79d2&quot;)\n    print(f&quot;\u4efb\u52a1\u7ed3\u679c: {results}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nasyncio.run(main_batch())<\/code><\/pre>\n<h4>5.3 \u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<p>\u534f\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c\uff0c\u63d0\u9ad8I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u6027\u80fd\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u5728\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528\nimport asyncio\nimport aiofiles\nimport time\n\nasync def write_file(filename, content):\n    &quot;&quot;&quot;\u5f02\u6b65\u5199\u5165\u6587\u4ef6&quot;&quot;&quot;\n    async with aiofiles.open(filename, 'w') as f:\n        await f.write(content)\n    print(f&quot;\u6587\u4ef6 {filename} \u5199\u5165\u5b8c\u6210&quot;)\n\nasync def read_file(filename):\n    &quot;&quot;&quot;\u5f02\u6b65\u8bfb\u53d6\u6587\u4ef6&quot;&quot;&quot;\n    async with aiofiles.open(filename, 'r') as f:\n        content = await f.read()\n    print(f&quot;\u6587\u4ef6 {filename} \u8bfb\u53d6\u5b8c\u6210\uff0c\u5185\u5bb9\u957f\u5ea6: {len(content)}&quot;)\n    return content\n\nasync def main():\n    &quot;&quot;&quot;\u4e3b\u534f\u7a0b&quot;&quot;&quot;\n    # \u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6\n    files = [f&quot;test{i}.txt&quot; for i in range(1, 6)]\n    contents = [f&quot;Content for file {i}\/n&quot; * 1000 for i in range(1, 6)]\n    \n    print(f&quot;\u5f00\u59cb\u5904\u7406 {len(files)} \u4e2a\u6587\u4ef6&quot;)\n    start_time = time.time()\n    \n    # \u5f02\u6b65\u5199\u5165\u6587\u4ef6\n    write_tasks = [write_file(files[i], contents[i]) for i in range(len(files))]\n    await asyncio.gather(*write_tasks)\n    \n    # \u5f02\u6b65\u8bfb\u53d6\u6587\u4ef6\n    read_tasks = [read_file(file) for file in files]\n    read_results = await asyncio.gather(*read_tasks)\n    \n    end_time = time.time()\n    print(f&quot;\/n\u6240\u6709\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\uff0c\u8017\u65f6: {end_time - start_time:.2f}\u79d2&quot;)\n    print(f&quot;\u8bfb\u53d6\u7684\u6587\u4ef6\u6570\u91cf: {len(read_results)}&quot;)\n\n# \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\nprint(&quot;\u6d4b\u8bd5\u534f\u7a0b\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c\uff1a&quot;)\nasyncio.run(main())<\/code><\/pre>\n<h4>5.4 \u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<p>\u534f\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u8bbf\u95ee\u7684\u5e76\u53d1\u6027\u80fd\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528\nimport asyncio\n\n# \u6ce8\u610f\uff1a\u9700\u8981\u5b89\u88c5 aiomysql \u5e93\ntry:\n    import aiomysql\n    \n    async def create_connection():\n        &quot;&quot;&quot;\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5&quot;&quot;&quot;\n        conn = await aiomysql.connect(\n            host='localhost',\n            port=3306,\n            user='root',\n            password='password',  # \u8bf7\u66ff\u6362\u4e3a\u5b9e\u9645\u5bc6\u7801\n            db='test',  # \u8bf7\u66ff\u6362\u4e3a\u5b9e\u9645\u6570\u636e\u5e93\n            loop=asyncio.get_event_loop()\n        )\n        return conn\n    \n    async def test_db():\n        &quot;&quot;&quot;\u6d4b\u8bd5\u6570\u636e\u5e93\u64cd\u4f5c&quot;&quot;&quot;\n        try:\n            # \u521b\u5efa\u8fde\u63a5\n            conn = await create_connection()\n            cursor = await conn.cursor()\n            \n            # \u521b\u5efa\u8868\n            await cursor.execute('''\n                CREATE TABLE IF NOT EXISTS users (\n                    id INT PRIMARY KEY AUTO_INCREMENT,\n                    name VARCHAR(255) NOT NULL,\n                    age INT NOT NULL\n                )\n            ''')\n            print(&quot;\u8868\u521b\u5efa\u6210\u529f&quot;)\n            \n            # \u63d2\u5165\u6570\u636e\n            users = [('Alice', 30), ('Bob', 25), ('Charlie', 35)]\n            await cursor.executemany(\n                'INSERT INTO users (name, age) VALUES (%s, %s)',\n                users\n            )\n            await conn.commit()\n            print(f&quot;\u63d2\u5165 {cursor.rowcount} \u6761\u6570\u636e&quot;)\n            \n            # \u67e5\u8be2\u6570\u636e\n            await cursor.execute('SELECT * FROM users')\n            results = await cursor.fetchall()\n            print(&quot;\u67e5\u8be2\u7ed3\u679c\uff1a&quot;)\n            for row in results:\n                print(row)\n            \n            # \u6e05\u7406\u6570\u636e\n            await cursor.execute('DELETE FROM users')\n            await conn.commit()\n            print(&quot;\u6570\u636e\u6e05\u7406\u5b8c\u6210&quot;)\n            \n        except Exception as e:\n            print(f&quot;\u6570\u636e\u5e93\u64cd\u4f5c\u9519\u8bef\uff1a{e}&quot;)\n        finally:\n            if 'cursor' in locals():\n                await cursor.close()\n            if 'conn' in locals():\n                conn.close()\n    \n    # \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\n    print(&quot;\u6d4b\u8bd5\u534f\u7a0b\u6570\u636e\u5e93\u64cd\u4f5c\uff1a&quot;)\n    asyncio.run(test_db())\n    \nexcept ImportError:\n    print(&quot;aiomysql \u5e93\u672a\u5b89\u88c5\uff0c\u8df3\u8fc7\u6570\u636e\u5e93\u6d4b\u8bd5&quot;)<\/code><\/pre>\n<h3>6. \u534f\u7a0b\u7684\u6027\u80fd\u8003\u8651<\/h3>\n<h4>6.1 \u6027\u80fd\u6d4b\u8bd5<\/h4>\n<p>\u8ba9\u6211\u4eec\u6d4b\u8bd5\u534f\u7a0b\u4e0e\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<pre><code class=\"python\"># \u534f\u7a0b\u7684\u6027\u80fd\u6d4b\u8bd5\nimport asyncio\nimport threading\nimport time\n\n# \u6d4b\u8bd5\u51fd\u6570\uff1a\u6a21\u62dfI\/O\u64cd\u4f5c\ndef io_operation(delay):\n    &quot;&quot;&quot;\u6a21\u62dfI\/O\u64cd\u4f5c&quot;&quot;&quot;\n    time.sleep(delay)\n\nasync def async_io_operation(delay):\n    &quot;&quot;&quot;\u5f02\u6b65\u6a21\u62dfI\/O\u64cd\u4f5c&quot;&quot;&quot;\n    await asyncio.sleep(delay)\n\n# \u6d4b\u8bd5\u7ebf\u7a0b\u6027\u80fd\ndef test_threads(count, delay):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u7ebf\u7a0b\u6027\u80fd&quot;&quot;&quot;\n    threads = []\n    for i in range(count):\n        t = threading.Thread(target=io_operation, args=(delay,))\n        threads.append(t)\n        t.start()\n    \n    for t in threads:\n        t.join()\n\n# \u6d4b\u8bd5\u534f\u7a0b\u6027\u80fd\nasync def test_coroutines(count, delay):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u534f\u7a0b\u6027\u80fd&quot;&quot;&quot;\n    tasks = []\n    for i in range(count):\n        task = asyncio.create_task(async_io_operation(delay))\n        tasks.append(task)\n    \n    await asyncio.gather(*tasks)\n\n# \u6d4b\u8bd5\u540c\u6b65\u6027\u80fd\ndef test_sync(count, delay):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u540c\u6b65\u6027\u80fd&quot;&quot;&quot;\n    for i in range(count):\n        io_operation(delay)\n\n# \u8fd0\u884c\u6027\u80fd\u6d4b\u8bd5\nprint(&quot;\u534f\u7a0b\u6027\u80fd\u6d4b\u8bd5\uff1a&quot;)\n\ncount = 1000\n delay = 0.01\n\n# \u6d4b\u8bd5\u540c\u6b65\nprint(f&quot;\/n\u6d4b\u8bd5\u540c\u6b65\u6267\u884c {count} \u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5ef6\u8fdf {delay} \u79d2&quot;)\nstart = time.time()\ntest_sync(count, delay)\nend = time.time()\nprint(f&quot;\u540c\u6b65\u6267\u884c\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u7ebf\u7a0b\nprint(f&quot;\/n\u6d4b\u8bd5\u7ebf\u7a0b\u6267\u884c {count} \u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5ef6\u8fdf {delay} \u79d2&quot;)\nstart = time.time()\ntest_threads(count, delay)\nend = time.time()\nprint(f&quot;\u7ebf\u7a0b\u6267\u884c\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u534f\u7a0b\nprint(f&quot;\/n\u6d4b\u8bd5\u534f\u7a0b\u6267\u884c {count} \u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5ef6\u8fdf {delay} \u79d2&quot;)\nstart = time.time()\nasyncio.run(test_coroutines(count, delay))\nend = time.time()\nprint(f&quot;\u534f\u7a0b\u6267\u884c\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u66f4\u5927\u7684\u4efb\u52a1\u91cf\ncount = 10000\n delay = 0.001\n\nprint(f&quot;\/n\u6d4b\u8bd5\u66f4\u5927\u7684\u4efb\u52a1\u91cf\uff1a{count} \u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5ef6\u8fdf {delay} \u79d2&quot;)\n\n# \u6d4b\u8bd5\u534f\u7a0b\nprint(&quot;\/n\u6d4b\u8bd5\u534f\u7a0b\u6267\u884c\uff1a&quot;)\nstart = time.time()\nasyncio.run(test_coroutines(count, delay))\nend = time.time()\nprint(f&quot;\u534f\u7a0b\u6267\u884c\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u7ebf\u7a0b\uff08\u6ce8\u610f\uff1a\u7ebf\u7a0b\u6570\u91cf\u8fc7\u591a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\uff09\nprint(f&quot;\/n\u6d4b\u8bd5\u7ebf\u7a0b\u6267\u884c\uff08\u4f7f\u7528 1000 \u4e2a\u7ebf\u7a0b\uff09\uff1a&quot;)\nstart = time.time()\ntest_threads(1000, delay * 10)  # \u51cf\u5c11\u7ebf\u7a0b\u6570\u91cf\uff0c\u589e\u52a0\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u5ef6\u8fdf\nend = time.time()\nprint(f&quot;\u7ebf\u7a0b\u6267\u884c\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)<\/code><\/pre>\n<h4>6.2 \u6027\u80fd\u4f18\u5316\u7b56\u7565<\/h4>\n<p>\u5728\u4f7f\u7528\u534f\u7a0b\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u7b56\u7565\u6765\u4f18\u5316\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u534f\u7a0b\u5207\u6362<\/strong>\uff1a\u907f\u514d\u8fc7\u591a\u7684\u534f\u7a0b\u5207\u6362\uff0c\u7279\u522b\u662f\u5728\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d<\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528\u4efb\u52a1\u5206\u7ec4<\/strong>\uff1a\u5bf9\u4e8e\u5927\u91cf\u7684\u534f\u7a0b\u4efb\u52a1\uff0c\u53ef\u4ee5\u5206\u7ec4\u5904\u7406\uff0c\u907f\u514d\u4e00\u6b21\u6027\u521b\u5efa\u8fc7\u591a\u7684\u4efb\u52a1<\/li>\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u5bf9\u4e8e\u7f51\u7edc\u3001\u6570\u636e\u5e93\u7b49\u8fde\u63a5\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u51cf\u5c11\u8fde\u63a5\u5efa\u7acb\u548c\u5173\u95ed\u7684\u5f00\u9500<\/li>\n<li><strong>\u4f18\u5316I\/O\u64cd\u4f5c<\/strong>\uff1a\u5c3d\u53ef\u80fd\u4f7f\u7528\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u907f\u514d\u963b\u585e\u534f\u7a0b\u6267\u884c<\/li>\n<li><strong>\u4f7f\u7528uvloop<\/strong>\uff1a\u5bf9\u4e8e\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528uvloop\u6765\u66ff\u4ee3\u9ed8\u8ba4\u7684\u4e8b\u4ef6\u5faa\u73af<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u534f\u7a0b\u6027\u80fd\u4f18\u5316\u7b56\u7565\u793a\u4f8b\nimport asyncio\nimport time\n\n# \u6d4b\u8bd5\u4e0d\u540c\u7684\u4efb\u52a1\u5206\u7ec4\u65b9\u5f0f\nasync def process_task(task_id):\n    &quot;&quot;&quot;\u5904\u7406\u4efb\u52a1&quot;&quot;&quot;\n    await asyncio.sleep(0.01)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    return task_id\n\nasync def process_batch(tasks):\n    &quot;&quot;&quot;\u5904\u7406\u6279\u6b21\u4efb\u52a1&quot;&quot;&quot;\n    return await asyncio.gather(*tasks)\n\nasync def main_no_batching():\n    &quot;&quot;&quot;\u4e0d\u4f7f\u7528\u6279\u6b21\u5904\u7406&quot;&quot;&quot;\n    tasks = [process_task(i) for i in range(10000)]\n    results = await asyncio.gather(*tasks)\n    return results\n\nasync def main_with_batching(batch_size=1000):\n    &quot;&quot;&quot;\u4f7f\u7528\u6279\u6b21\u5904\u7406&quot;&quot;&quot;\n    tasks = [process_task(i) for i in range(10000)]\n    results = []\n    \n    for i in range(0, len(tasks), batch_size):\n        batch = tasks[i:i + batch_size]\n        batch_results = await process_batch(batch)\n        results.extend(batch_results)\n    \n    return results\n\n# \u8fd0\u884c\u6027\u80fd\u6d4b\u8bd5\nprint(&quot;\u534f\u7a0b\u6027\u80fd\u4f18\u5316\u7b56\u7565\u6d4b\u8bd5\uff1a&quot;)\n\n# \u6d4b\u8bd5\u4e0d\u4f7f\u7528\u6279\u6b21\u5904\u7406\nprint(&quot;\/n\u6d4b\u8bd5\u4e0d\u4f7f\u7528\u6279\u6b21\u5904\u7406\uff1a&quot;)\nstart = time.time()\nasyncio.run(main_no_batching())\nend = time.time()\nprint(f&quot;\u4e0d\u4f7f\u7528\u6279\u6b21\u5904\u7406\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u4f7f\u7528\u6279\u6b21\u5904\u7406\nprint(&quot;\/n\u6d4b\u8bd5\u4f7f\u7528\u6279\u6b21\u5904\u7406\uff1a&quot;)\nstart = time.time()\nasyncio.run(main_with_batching())\nend = time.time()\nprint(f&quot;\u4f7f\u7528\u6279\u6b21\u5904\u7406\u8017\u65f6\uff1a{end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u4e0d\u540c\u6279\u6b21\u5927\u5c0f\nprint(&quot;\/n\u6d4b\u8bd5\u4e0d\u540c\u6279\u6b21\u5927\u5c0f\uff1a&quot;)\nbatch_sizes = [100, 500, 1000, 2000, 5000]\n\nfor batch_size in batch_sizes:\n    start = time.time()\n    asyncio.run(main_with_batching(batch_size))\n    end = time.time()\n    print(f&quot;\u6279\u6b21\u5927\u5c0f {batch_size}\uff1a{end - start:.4f}\u79d2&quot;)<\/code><\/pre>\n<h3>7. \u5b9e\u8df5\u6848\u4f8b\uff1a\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65Web\u670d\u52a1\u5668<\/h3>\n<h4>7.1 \u6848\u4f8b\u6982\u8ff0<\/h4>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684 <code>asyncio<\/code> \u548c <code>aiohttp<\/code> \u5e93\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65Web\u670d\u52a1\u5668\uff0c\u5c55\u793a\u534f\u7a0b\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<h4>7.2 \u5b9e\u73b0\u4ee3\u7801<\/h4>\n<pre><code class=\"python\"># \u5b9e\u73b0\u5f02\u6b65Web\u670d\u52a1\u5668\nimport asyncio\nfrom aiohttp import web\nimport time\n\n# \u5904\u7406\u51fd\u6570\uff1a\u9996\u9875\nasync def handle_index(request):\n    &quot;&quot;&quot;\u5904\u7406\u9996\u9875\u8bf7\u6c42&quot;&quot;&quot;\n    return web.Response(text=&quot;Hello, Async Web Server!&quot;)\n\n# \u5904\u7406\u51fd\u6570\uff1a\u5ef6\u8fdf\u54cd\u5e94\nasync def handle_delay(request):\n    &quot;&quot;&quot;\u5904\u7406\u5ef6\u8fdf\u54cd\u5e94\u8bf7\u6c42&quot;&quot;&quot;\n    # \u83b7\u53d6\u5ef6\u8fdf\u53c2\u6570\n    delay = float(request.match_info.get('delay', 1))\n    print(f&quot;\u5904\u7406\u5ef6\u8fdf\u8bf7\u6c42\uff0c\u5ef6\u8fdf {delay} \u79d2&quot;)\n    # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    await asyncio.sleep(delay)\n    return web.Response(text=f&quot;Delayed response after {delay} seconds&quot;)\n\n# \u5904\u7406\u51fd\u6570\uff1a\u5e76\u53d1\u6d4b\u8bd5\nasync def handle_concurrent(request):\n    &quot;&quot;&quot;\u5904\u7406\u5e76\u53d1\u6d4b\u8bd5\u8bf7\u6c42&quot;&quot;&quot;\n    # \u83b7\u53d6\u5e76\u53d1\u6570\u53c2\u6570\n    count = int(request.match_info.get('count', 10))\n    print(f&quot;\u5904\u7406\u5e76\u53d1\u6d4b\u8bd5\u8bf7\u6c42\uff0c\u5e76\u53d1\u6570 {count}&quot;)\n    \n    # \u521b\u5efa\u5e76\u53d1\u4efb\u52a1\n    async def task(i):\n        await asyncio.sleep(0.1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n        return i\n    \n    # \u6267\u884c\u5e76\u53d1\u4efb\u52a1\n    tasks = [task(i) for i in range(count)]\n    results = await asyncio.gather(*tasks)\n    \n    return web.Response(text=f&quot;Concurrent tasks completed: {results}&quot;)\n\n# \u5904\u7406\u51fd\u6570\uff1a\u72b6\u6001\u4fe1\u606f\nasync def handle_status(request):\n    &quot;&quot;&quot;\u5904\u7406\u72b6\u6001\u4fe1\u606f\u8bf7\u6c42&quot;&quot;&quot;\n    # \u83b7\u53d6\u4e8b\u4ef6\u5faa\u73af\u4fe1\u606f\n    loop = asyncio.get_event_loop()\n    stats = {\n        &quot;loop&quot;: type(loop).__name__,\n        &quot;time&quot;: time.strftime('%Y-%m-%d %H:%M:%S'),\n        &quot;uptime&quot;: f&quot;{time.time() - start_time:.2f} seconds&quot;\n    }\n    return web.json_response(stats)\n\n# \u521d\u59cb\u5316\u670d\u52a1\u5668\nasync def init_app():\n    &quot;&quot;&quot;\u521d\u59cb\u5316\u5e94\u7528&quot;&quot;&quot;\n    app = web.Application()\n    # \u6ce8\u518c\u8def\u7531\n    app.add_routes([\n        web.get('\/', handle_index),\n        web.get('\/delay\/{delay}', handle_delay),\n        web.get('\/concurrent\/{count}', handle_concurrent),\n        web.get('\/status', handle_status)\n    ])\n    return app\n\n# \u5168\u5c40\u53d8\u91cf\uff1a\u670d\u52a1\u5668\u542f\u52a8\u65f6\u95f4\nstart_time = time.time()\n\n# \u542f\u52a8\u670d\u52a1\u5668\nprint(&quot;\u542f\u52a8\u5f02\u6b65Web\u670d\u52a1\u5668\uff1a&quot;)\nprint(&quot;\u8bbf\u95ee\u5730\u5740\uff1ahttp:\/\/localhost:8080&quot;)\nprint(&quot;\u6d4b\u8bd5\u8def\u5f84\uff1a&quot;)\nprint(&quot;  \/              - \u9996\u9875&quot;)\nprint(&quot;  \/delay\/{\u79d2\u6570}  - \u5ef6\u8fdf\u54cd\u5e94\u6d4b\u8bd5&quot;)\nprint(&quot;  \/concurrent\/{\u6570\u91cf} - \u5e76\u53d1\u6d4b\u8bd5&quot;)\nprint(&quot;  \/status        - \u670d\u52a1\u5668\u72b6\u6001&quot;)\nprint(&quot;\/n\u6309 Ctrl+C \u505c\u6b62\u670d\u52a1\u5668&quot;)\n\n# \u8fd0\u884c\u670d\u52a1\u5668\nweb.run_app(init_app(), port=8080)<\/code><\/pre>\n<h4>7.3 \u5e94\u7528\u573a\u666f<\/h4>\n<p>\u5f02\u6b65Web\u670d\u52a1\u5668\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u5e76\u53d1\u8bf7\u6c42<\/strong>\uff1a\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1HTTP\u8bf7\u6c42<\/li>\n<li><strong>I\/O\u5bc6\u96c6\u578b\u64cd\u4f5c<\/strong>\uff1a\u5982\u6570\u636e\u5e93\u67e5\u8be2\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49<\/li>\n<li><strong>\u5b9e\u65f6\u5e94\u7528<\/strong>\uff1a\u5982\u804a\u5929\u5e94\u7528\u3001\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u7b49<\/li>\n<li><strong>API\u670d\u52a1<\/strong>\uff1a\u63d0\u4f9bRESTful API\u670d\u52a1<\/li>\n<li><strong>\u5fae\u670d\u52a1\u67b6\u6784<\/strong>\uff1a\u4f5c\u4e3a\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u670d\u52a1\u8282\u70b9<\/li>\n<\/ul>\n<h3>8. \u603b\u7ed3<\/h3>\n<p>\u672c\u6587\u8be6\u7ec6\u5206\u6790\u4e86Python\u4e2d\u7684\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u534f\u7a0b\u7684\u6982\u5ff5\u4e0e\u57fa\u672c\u539f\u7406<\/strong>\uff1a\u534f\u7a0b\u7684\u5b9a\u4e49\u3001\u5de5\u4f5c\u539f\u7406\u548c\u4f18\u52bf<\/li>\n<li><strong>Python\u4e2d\u7684\u534f\u7a0b\u5b9e\u73b0<\/strong>\uff1a\u751f\u6210\u5668\u534f\u7a0b\u3001\u539f\u751f\u534f\u7a0b\u548c\u534f\u7a0b\u88c5\u9970\u5668<\/li>\n<li><strong>\u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u539f\u7406<\/strong>\uff1a\u4e8b\u4ef6\u5faa\u73af\u7684\u6982\u5ff5\u3001\u5de5\u4f5c\u6d41\u7a0b\u548c\u7c7b\u578b<\/li>\n<li><strong>\u534f\u7a0b\u7684\u9ad8\u7ea7\u7279\u6027<\/strong>\uff1a\u534f\u7a0b\u7684\u53d6\u6d88\u3001\u8d85\u65f6\u5904\u7406\u3001\u5f02\u5e38\u5904\u7406\u548c\u5d4c\u5957<\/li>\n<li><strong>\u534f\u7a0b\u7684\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u7f51\u7edc\u7f16\u7a0b\u3001\u5e76\u53d1\u4efb\u52a1\u5904\u7406\u3001\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c\u548c\u6570\u636e\u5e93\u64cd\u4f5c<\/li>\n<li><strong>\u534f\u7a0b\u7684\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u6027\u80fd\u6d4b\u8bd5\u548c\u4f18\u5316\u7b56\u7565<\/li>\n<li><strong>\u5b9e\u8df5\u6848\u4f8b<\/strong>\uff1a\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65Web\u670d\u52a1\u5668<\/li>\n<\/ul>\n<p>\u534f\u7a0b\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u7684\u5e76\u53d1\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u5728\u5355\u7ebf\u7a0b\u5185\u5b9e\u73b0\u5e76\u53d1\u64cd\u4f5c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u5904\u7406\u6548\u7387\u3002\u4e0e\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u76f8\u6bd4\uff0c\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u975e\u5e38\u5c0f\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u4efb\u52a1\u3002<\/p>\n<p>\u5728Python 3.5+\u4e2d\uff0c\u4f7f\u7528 <code>async\/await<\/code> \u8bed\u6cd5\u53ef\u4ee5\u66f4\u7b80\u6d01\u3001\u66f4\u6e05\u6670\u5730\u7f16\u5199\u534f\u7a0b\u4ee3\u7801\u3002\u7ed3\u5408 <code>asyncio<\/code> \u5e93\u63d0\u4f9b\u7684\u4e8b\u4ef6\u5faa\u73af\u548c\u5404\u79cd\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u8bfb\u8005\u5e94\u8be5\u80fd\u591f\uff1a<\/p>\n<ol>\n<li>\u7406\u89e3\u534f\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u638c\u63e1Python\u4e2d\u534f\u7a0b\u7684\u5b9e\u73b0\u65b9\u5f0f\u548c\u4f7f\u7528\u65b9\u6cd5<\/li>\n<li>\u4e86\u89e3\u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u4f5c\u539f\u7406\u548c\u7c7b\u578b<\/li>\n<li>\u638c\u63e1\u534f\u7a0b\u7684\u9ad8\u7ea7\u7279\u6027\u548c\u5e94\u7528\u573a\u666f<\/li>\n<li>\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u534f\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u5e76\u53d1\u80fd\u529b<\/li>\n<\/ol>\n<p>\u534f\u7a0b\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u6709\u524d\u9014\u7684\u5e76\u53d1\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002\u5728\u672a\u6765\u7684Python\u5f00\u53d1\u4e2d\uff0c\u534f\u7a0b\u5c06\u4f1a\u88ab\u8d8a\u6765\u8d8a\u5e7f\u6cdb\u5730\u5e94\u7528\uff0c\u7279\u522b\u662f\u5728\u7f51\u7edc\u7f16\u7a0b\u3001\u6570\u636e\u5904\u7406\u7b49I\/O\u5bc6\u96c6\u578b\u573a\u666f\u4e2d\u3002<\/p>\n<h3>9. \u53c2\u8003\u6587\u732e<\/h3>\n<ol>\n<li>Python Documentation: Coroutines and Tasks<\/li>\n<li>Python Documentation: Event Loops<\/li>\n<li>PEP 492 &#8212; Coroutines with async and await syntax<\/li>\n<li>PEP 380 &#8212; Syntax for Delegating to a Subgenerator<\/li>\n<li>Async IO in Python: A Complete Walkthrough &#8211; Real Python<\/li>\n<li>Effective Python: 90 Specific Ways to Write Better Python &#8211; Addison-Wesley<\/li>\n<li>Python Cookbook, 3rd Edition &#8211; O&#8217;Reilly Media<\/li>\n<li>Fluent Python &#8211; O&#8217;Reilly Media<\/li>\n<li>High Performance Python &#8211; O&#8217;Reilly Media<\/li>\n<li>aiohttp Documentation<\/li>\n<\/ol>\n<h3>10. \u7ed3\u8bed<\/h3>\n<p>\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\u662fPython\u4e2d\u5b9e\u73b0\u9ad8\u6548\u5e76\u53d1\u7f16\u7a0b\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u4eec\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u5904\u7406\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5355\u7ebf\u7a0b\u5185\u5b9e\u73b0\u5e76\u53d1\u64cd\u4f5c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u5904\u7406\u6548\u7387\u3002<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u534f\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5b9e\u73b0\u65b9\u5f0f\u3001\u9ad8\u7ea7\u7279\u6027\u548c\u5e94\u7528\u573a\u666f\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5b9e\u8df5\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u534f\u7a0b\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u534f\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u638c\u63e1\u534f\u7a0b\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u6709\u6548\u5730\u5e94\u7528\u534f\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n<p>\u5728Python\u7684\u672a\u6765\u53d1\u5c55\u4e2d\uff0c\u534f\u7a0b\u5c06\u4f1a\u626e\u6f14\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u89d2\u8272\uff0c\u7279\u522b\u662f\u968f\u7740\u5f02\u6b65I\/O\u5e93\u7684\u4e0d\u65ad\u5b8c\u5584\u548c\u666e\u53ca\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u9ad8\u6548\u3001\u7b80\u6d01\u7684Python\u4ee3\u7801\uff0c\u5e94\u5bf9\u65e5\u76ca\u590d\u6742\u7684\u5e76\u53d1\u7f16\u7a0b\u9700\u6c42\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585252\">https:\/\/segmentfault.com\/a\/1190000047585252<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u673a\u5236 1. \u534f\u7a0b\u7684\u6982\u5ff5\u4e0e\u57fa\u672c\u539f\u7406 \u534f\u7a0b\uff08Coroutine\uff09\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021231","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021231"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021231\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}