{"id":2842,"date":"2025-03-16T12:24:42","date_gmt":"2025-03-16T04:24:42","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2842"},"modified":"2025-03-16T12:24:42","modified_gmt":"2025-03-16T04:24:42","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b11%ef%bc%9a%e5%bc%82%e6%ad%a5%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b11%ef%bc%9a%e5%bc%82%e6%ad%a5%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b11\uff1a\u5f02\u6b65\u6587\u4ef6\u5904\u7406\u7cfb\u7edf"},"content":{"rendered":"\n<p>\u5728\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u65f6\uff0c\u6587\u4ef6\u64cd\u4f5c\uff08\u5982\u8bfb\u5199\u6587\u4ef6\u3001\u4e0a\u4f20\u4e0b\u8f7d\u6587\u4ef6\u7b49\uff09\u901a\u5e38\u662f\u74f6\u9888\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6587\u4ef6\u6216\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\u3002\u901a\u8fc7\u5f02\u6b65\u6587\u4ef6\u5904\u7406\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6587\u4ef6\u64cd\u4f5c\u7684\u6548\u7387\u548c\u5e94\u7528\u7684\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11.1 \u5f02\u6b65\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>asyncio<\/code> \u548c\u5f02\u6b65\u6587\u4ef6 I\/O \u5e93\u6765\u6267\u884c\u5f02\u6b65\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3002\u901a\u5e38\uff0c<code>aiofiles<\/code> \u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5e93\uff0c\u5b83\u4f7f\u5f97\u6587\u4ef6\u64cd\u4f5c\u652f\u6301\u5f02\u6b65\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.1.1 \u5b89\u88c5 <code>aiofiles<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install aiofiles<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.1.2 \u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiofiles\nimport asyncio\n\nasync def read_file():\n    async with aiofiles.open('example.txt', mode='r') as file:\n        content = await file.read()\n        print(\"\u6587\u4ef6\u5185\u5bb9:\", content)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(read_file())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.1.3 \u5f02\u6b65\u6587\u4ef6\u5199\u5165<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiofiles\nimport asyncio\n\nasync def write_file():\n    async with aiofiles.open('output.txt', mode='w') as file:\n        await file.write(\"Hello, world!\")\n        print(\"\u6587\u4ef6\u5199\u5165\u6210\u529f\")\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(write_file())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11.2 \u5f02\u6b65\u6587\u4ef6\u4e0a\u4f20\u4e0e\u4e0b\u8f7d<\/strong><\/h2>\n\n\n\n<p>\u5f02\u6b65\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u5e38\u7528\u4e8e Web \u5e94\u7528\u4e2d\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u3002<code>aiohttp<\/code> \u5e93\u63d0\u4f9b\u4e86\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u9ad8\u6548\u7684\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u529f\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.2.1 \u4f7f\u7528 <code>aiohttp<\/code> \u5f02\u6b65\u4e0a\u4f20\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5c06\u6587\u4ef6\u901a\u8fc7 HTTP \u8bf7\u6c42\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>aiohttp<\/code> \u63d0\u4f9b\u7684\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f02\u6b65\u4e0a\u4f20\u6587\u4ef6\u7684\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiohttp\nimport asyncio\n\nasync def upload_file():\n    url = 'http:\/\/example.com\/upload'\n    data = {'file': open('example.txt', 'rb')}\n    async with aiohttp.ClientSession() as session:\n        async with session.post(url, data=data) as response:\n            print(\"\u4e0a\u4f20\u54cd\u5e94:\", await response.text())\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(upload_file())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.2.2 \u4f7f\u7528 <code>aiohttp<\/code> \u5f02\u6b65\u4e0b\u8f7d\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5f02\u6b65\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f02\u6b65\u4e0b\u8f7d\u6587\u4ef6\u7684\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiohttp\nimport asyncio\n\nasync def download_file():\n    url = 'http:\/\/example.com\/file.zip'\n    async with aiohttp.ClientSession() as session:\n        async with session.get(url) as response:\n            with open('downloaded_file.zip', 'wb') as f:\n                while True:\n                    chunk = await response.content.read(1024)\n                    if not chunk:\n                        break\n                    f.write(chunk)\n            print(\"\u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210\")\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(download_file())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11.3 \u5f02\u6b65\u6587\u4ef6\u5904\u7406\u7684\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.3.1 \u5f02\u6b65\u6279\u91cf\u6587\u4ef6\u8bfb\u53d6<\/strong><\/h3>\n\n\n\n<p>\u5f53\u9700\u8981\u4ece\u591a\u4e2a\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>asyncio.gather()<\/code> \u6765\u5e76\u53d1\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiofiles\nimport asyncio\n\nasync def read_multiple_files(files):\n    async def read_file(file_path):\n        async with aiofiles.open(file_path, mode='r') as file:\n            content = await file.read()\n            return content\n\n    tasks = &#91;read_file(file) for file in files]\n    contents = await asyncio.gather(*tasks)\n    return contents\n\nfiles = &#91;'file1.txt', 'file2.txt', 'file3.txt']\nloop = asyncio.get_event_loop()\ncontents = loop.run_until_complete(read_multiple_files(files))\nprint(contents)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.3.2 \u5f02\u6b65\u6279\u91cf\u6587\u4ef6\u5199\u5165<\/strong><\/h3>\n\n\n\n<p>\u5f53\u9700\u8981\u5411\u591a\u4e2a\u6587\u4ef6\u5199\u5165\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u6279\u91cf\u5199\u5165\u65b9\u6cd5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiofiles\nimport asyncio\n\nasync def write_multiple_files(file_data):\n    async def write_file(file_path, data):\n        async with aiofiles.open(file_path, mode='w') as file:\n            await file.write(data)\n\n    tasks = &#91;write_file(file, data) for file, data in file_data.items()]\n    await asyncio.gather(*tasks)\n\nfile_data = {\n    'file1.txt': 'Content for file 1',\n    'file2.txt': 'Content for file 2',\n    'file3.txt': 'Content for file 3',\n}\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(write_multiple_files(file_data))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11.4 \u5f02\u6b65\u6587\u4ef6\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7f13\u51b2\u533a\u5927\u5c0f<\/strong>\uff1a\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u589e\u52a0\u7f13\u51b2\u533a\u5927\u5c0f\u53ef\u4ee5\u51cf\u5c11 I\/O \u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u5206\u5757\u4e0a\u4f20\/\u4e0b\u8f7d<\/strong>\uff1a\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5206\u5757\u4e0a\u4f20\/\u4e0b\u8f7d\uff0c\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u4efb\u52a1\u6c60<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u5904\u7406\u5927\u91cf\u6587\u4ef6\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u6c60\u6765\u63a7\u5236\u5e76\u53d1\u6570\uff0c\u907f\u514d\u8fc7\u591a\u4efb\u52a1\u540c\u65f6\u6267\u884c\u800c\u5bfc\u81f4\u8d44\u6e90\u8017\u5c3d\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.4.1 \u5f02\u6b65\u6587\u4ef6\u5904\u7406\u4efb\u52a1\u6c60<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nfrom asyncio import Semaphore\n\nsemaphore = Semaphore(5)  # \u9650\u5236\u6700\u5927\u5e76\u53d1\u6570\u4e3a5\n\nasync def process_file(file):\n    async with semaphore:\n        # \u6a21\u62df\u6587\u4ef6\u5904\u7406\n        await asyncio.sleep(1)\n        print(f\"\u6587\u4ef6\u5904\u7406\u5b8c\u6210: {file}\")\n\nasync def process_files(files):\n    tasks = &#91;process_file(file) for file in files]\n    await asyncio.gather(*tasks)\n\nfiles = &#91;'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt']\nloop = asyncio.get_event_loop()\nloop.run_until_complete(process_files(files))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>aiofiles<\/code> \u8fdb\u884c\u5f02\u6b65\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u6587\u4ef6 I\/O \u7684\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5229\u7528 <code>aiohttp<\/code> \u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7684\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u6279\u91cf\u5904\u7406\u3001\u4efb\u52a1\u6c60\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u9002\u5e94\u5927\u89c4\u6a21\u6587\u4ef6\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u5f02\u6b65\u961f\u5217\u548c\u4efb\u52a1\u8c03\u5ea6\u7cfb\u7edf\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u65f6\uff0c\u6587\u4ef6\u64cd\u4f5c\uff08\u5982\u8bfb\u5199\u6587\u4ef6\u3001\u4e0a\u4f20\u4e0b\u8f7d\u6587\u4ef6\u7b49\uff09\u901a\u5e38\u662f\u74f6\u9888\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6587\u4ef6\u6216\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2842"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2842\/revisions"}],"predecessor-version":[{"id":2844,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2842\/revisions\/2844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2843"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}