{"id":1098199,"date":"2025-01-08T15:19:01","date_gmt":"2025-01-08T07:19:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098199.html"},"modified":"2025-01-08T15:19:03","modified_gmt":"2025-01-08T07:19:03","slug":"python%e5%a6%82%e4%bd%95%e5%a2%9e%e5%bf%ab%e8%af%bb%e5%86%99%e6%96%87%e4%bb%b6%e7%9a%84%e9%80%9f%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098199.html","title":{"rendered":"python\u5982\u4f55\u589e\u5feb\u8bfb\u5199\u6587\u4ef6\u7684\u901f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212621\/82841cf8-fb31-4085-9c3f-8da81d345a61.webp\" alt=\"python\u5982\u4f55\u589e\u5feb\u8bfb\u5199\u6587\u4ef6\u7684\u901f\u5ea6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u3001\u4f18\u5316\u6587\u4ef6\u8bfb\u53d6\u65b9\u5f0f\u3001\u51cf\u5c11\u78c1\u76d8 I\/O \u64cd\u4f5c\u3001\u589e\u52a0\u7f13\u5b58\u6216\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/strong>\u662f\u4e00\u79cd\u6781\u4e3a\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6587\u4ef6\u8bfb\u5199\u901f\u5ea6\u3002\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08Memory-Mapped Files\uff09\u5141\u8bb8\u6211\u4eec\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u5927\u5927\u52a0\u5feb\u5bf9\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528 <code>mmap<\/code> \u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u8bbf\u95ee\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u907f\u514d\u9891\u7e41\u7684\u78c1\u76d8 I\/O \u64cd\u4f5c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h2>\n<\/p>\n<p><h3>\u591a\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u6587\u4ef6\u8bfb\u5199\u7684\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5f53\u6587\u4ef6\u64cd\u4f5c\u4e0d\u662f\u5355\u4e00\u7684\u987a\u5e8f\u64cd\u4f5c\u65f6\u3002Python \u7684 <code>threading<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u591a\u7ebf\u7a0b\u652f\u6301\u3002\u867d\u7136 Python \u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\uff0c\u4f46\u5bf9\u4e8e I\/O \u5bc6\u96c6\u578b\u64cd\u4f5c\uff0c\u591a\u7ebf\u7a0b\u4ecd\u7136\u662f\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<p>def write_file(file_path, data):<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p>thread1 = threading.Thread(target=read_file, args=(&#39;file1.txt&#39;,))<\/p>\n<p>thread2 = threading.Thread(target=write_file, args=(&#39;file2.txt&#39;, &#39;some data&#39;))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u76f8\u6bd4\u591a\u7ebf\u7a0b\uff0c\u591a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d GIL \u7684\u9650\u5236\uff0c\u4ece\u800c\u66f4\u597d\u5730\u5229\u7528\u591a\u6838 CPU \u7684\u4f18\u52bf\u3002Python \u63d0\u4f9b\u4e86 <code>multiprocessing<\/code> \u6a21\u5757\u6765\u652f\u6301\u591a\u8fdb\u7a0b\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<p>def write_file(file_path, data):<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p>process1 = multiprocessing.Process(target=read_file, args=(&#39;file1.txt&#39;,))<\/p>\n<p>process2 = multiprocessing.Process(target=write_file, args=(&#39;file2.txt&#39;, &#39;some data&#39;))<\/p>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f18\u5316\u6587\u4ef6\u8bfb\u53d6\u65b9\u5f0f<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u5408\u9002\u7684\u7f13\u51b2\u533a\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u5408\u9002\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8bfb\u53d6\u901f\u5ea6\u3002\u901a\u8fc7\u8c03\u6574\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u53ef\u4ee5\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">buffer_size = 1024 * 1024  # 1MB<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;, buffering=buffer_size) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(buffer_size)<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        # Process the chunk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u4ece\u800c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u51cf\u5c11\u78c1\u76d8 I\/O \u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>\u6279\u91cf\u8bfb\u5199<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u51cf\u5c11\u5bf9\u78c1\u76d8\u7684\u9891\u7e41\u8bfb\u5199\u64cd\u4f5c\uff0c\u901a\u8fc7\u6279\u91cf\u5904\u7406\u6570\u636e\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5199\u64cd\u4f5c\u5408\u5e76\u4e3a\u4e00\u6b21\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_to_write = []<\/p>\n<p>for i in range(1000):<\/p>\n<p>    data_to_write.append(f&quot;Line {i}\\n&quot;)<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(data_to_write)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5185\u5b58\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u7f13\u5b58\u5230\u5185\u5b58\u4e2d\uff0c\u907f\u514d\u9891\u7e41\u7684\u78c1\u76d8 I\/O \u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528 Python \u7684 <code>io<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\u5185\u5b58\u7f13\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>buffer = io.StringIO()<\/p>\n<p>for i in range(1000):<\/p>\n<p>    buffer.write(f&quot;Line {i}\\n&quot;)<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(buffer.getvalue())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u589e\u52a0\u7f13\u5b58\u6216\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u5141\u8bb8\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u5927\u5927\u52a0\u5feb\u6587\u4ef6\u7684\u8bfb\u5199\u901f\u5ea6\u3002Python \u63d0\u4f9b\u4e86 <code>mmap<\/code> \u6a21\u5757\u6765\u652f\u6301\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r+b&#39;) as f:<\/p>\n<p>    mmapped_file = mmap.mmap(f.fileno(), 0)<\/p>\n<p>    print(mmapped_file.read(100))  # Read first 100 bytes<\/p>\n<p>    mmapped_file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>lru_cache<\/code><\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u91cd\u590d\u8bfb\u53d6\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>functools.lru_cache<\/code> \u6765\u7f13\u5b58\u8bfb\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=128)<\/p>\n<p>def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<p>data = read_file(&#39;file.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u3001\u4f18\u5316\u6587\u4ef6\u8bfb\u53d6\u65b9\u5f0f\u3001\u51cf\u5c11\u78c1\u76d8 I\/O \u64cd\u4f5c\u548c\u589e\u52a0\u7f13\u5b58\u6216\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 Python \u8bfb\u5199\u6587\u4ef6\u7684\u901f\u5ea6\u3002\u5177\u4f53\u65b9\u6cd5\u7684\u9009\u62e9\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u573a\u666f\u8fdb\u884c\u8c03\u6574\u3002\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u5c24\u5176\u6709\u6548\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5f80\u5f80\u53ef\u4ee5\u53d6\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u5347\u6587\u4ef6\u8bfb\u53d6\u901f\u5ea6\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u5347\u6587\u4ef6\u8bfb\u53d6\u901f\u5ea6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e9b\u4f18\u5316\u6280\u672f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u53ef\u4ee5\u786e\u4fdd\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>mmap<\/code>\u6a21\u5757\u5c06\u6587\u4ef6\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u52a0\u5feb\u8bfb\u53d6\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u91c7\u7528\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\uff08\u5982\u4e8c\u8fdb\u5236\u683c\u5f0f\uff09\u4e5f\u80fd\u663e\u8457\u63d0\u9ad8\u8bfb\u53d6\u6548\u7387\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u6ca1\u6709\u63a8\u8350\u7684\u5e93\u6765\u52a0\u5feb\u6587\u4ef6\u5199\u5165\u901f\u5ea6\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u6709\u6548\u52a0\u5feb\u6587\u4ef6\u5199\u5165\u901f\u5ea6\u3002<code>pandas<\/code>\u5e93\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u4f18\u5f02\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528<code>to_csv<\/code>\u6216<code>to_parquet<\/code>\u7b49\u65b9\u6cd5\u65f6\u3002\u6b64\u5916\uff0c<code>dask<\/code>\u5e93\u5141\u8bb8\u5e76\u884c\u5904\u7406\u6570\u636e\uff0c\u4f7f\u5f97\u5199\u5165\u64cd\u4f5c\u66f4\u5feb\u3002\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08<code>mmap<\/code>\uff09\u4e5f\u80fd\u63d0\u9ad8\u5199\u5165\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u4ee5\u63d0\u9ad8\u8bfb\u5199\u6027\u80fd\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u5bf9\u4e8e\u63d0\u9ad8\u8bfb\u5199\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u4f8b\u5982\uff0cCSV\u6587\u4ef6\u9002\u5408\u5b58\u50a8\u6587\u672c\u6570\u636e\uff0c\u4f46\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u4e8c\u8fdb\u5236\u683c\u5f0f\uff08\u5982Parquet\u6216HDF5\uff09\u901a\u5e38\u4f1a\u66f4\u5feb\u3002\u4e8c\u8fdb\u5236\u683c\u5f0f\u4e0d\u4ec5\u51cf\u5c11\u4e86\u6587\u4ef6\u5927\u5c0f\uff0c\u8fd8\u80fd\u52a0\u5feb\u8bfb\u5199\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u6839\u636e\u5177\u4f53\u5e94\u7528\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7b97\u6cd5\u4e5f\u80fd\u5728\u6027\u80fd\u548c\u5b58\u50a8\u7a7a\u95f4\u4e4b\u95f4\u53d6\u5f97\u826f\u597d\u7684\u5e73\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u3001\u4f18\u5316\u6587\u4ef6\u8bfb\u53d6\u65b9\u5f0f\u3001\u51cf\u5c11\u78c1\u76d8 I\/O \u64cd\u4f5c\u3001\u589e\u52a0\u7f13\u5b58\u6216\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5185 [&hellip;]","protected":false},"author":3,"featured_media":1098206,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098199"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1098199"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098199\/revisions"}],"predecessor-version":[{"id":1098207,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098199\/revisions\/1098207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098206"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}