{"id":981026,"date":"2024-12-27T06:59:07","date_gmt":"2024-12-26T22:59:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981026.html"},"modified":"2024-12-27T06:59:09","modified_gmt":"2024-12-26T22:59:09","slug":"python%e5%a6%82%e4%bd%95%e8%b7%91%e6%bb%a1cpu","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981026.html","title":{"rendered":"python\u5982\u4f55\u8dd1\u6ee1cpu"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210145\/65154f12-2aa7-4924-aa00-f81e11e079d1.webp\" alt=\"python\u5982\u4f55\u8dd1\u6ee1cpu\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u8ba9\u7a0b\u5e8f\u8dd1\u6ee1CPU\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u5e93\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u591a\u8fdb\u7a0b\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3001\u7ebf\u7a0b\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3001\u5e76\u884c\u8ba1\u7b97\u5e93\u5982NumPy\u548cDask\u9002\u5408\u6570\u503c\u8ba1\u7b97\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u591a\u8fdb\u7a0b\u5b9e\u73b0CPU\u8dd1\u6ee1<\/p>\n<\/p>\n<p><p>Python\u7684GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u6267\u884c\u6548\u7387\uff0c\u56e0\u6b64\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u591a\u8fdb\u7a0b\u662f\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u591a\u8fdb\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\u548c\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u6837\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528multiprocessing\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684multiprocessing\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def cpu_bound_task(n):<\/p>\n<p>    total = 0<\/p>\n<p>    for i in range(10000000):<\/p>\n<p>        total += i * n<\/p>\n<p>    return total<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    processes = []<\/p>\n<p>    for i in range(multiprocessing.cpu_count()):<\/p>\n<p>        process = multiprocessing.Process(target=cpu_bound_task, args=(i,))<\/p>\n<p>        processes.append(process)<\/p>\n<p>        process.start()<\/p>\n<p>    for process in processes:<\/p>\n<p>        process.join()<\/p>\n<p>    print(&quot;Time taken: &quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5e76\u4e3a\u6bcf\u4e2aCPU\u6838\u5fc3\u542f\u52a8\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u4ece\u800c\u5b9e\u73b0CPU\u8dd1\u6ee1\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u591a\u7ebf\u7a0b\u5b9e\u73b0CPU\u8dd1\u6ee1<\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u9002\u5408\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002\u867d\u7136GIL\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u7684\u6267\u884c\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u591a\u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>\u7ebf\u7a0b\u662f\u6bd4\u8fdb\u7a0b\u66f4\u5c0f\u7684\u6267\u884c\u5355\u5143\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u9002\u5408\u5904\u7406\u9700\u8981\u9891\u7e41\u7b49\u5f85\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528threading\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684threading\u5e93\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u7ebf\u7a0b\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u7ebf\u7a0b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def io_bound_task():<\/p>\n<p>    time.sleep(1)<\/p>\n<p>threads = []<\/p>\n<p>start_time = time.time()<\/p>\n<p>for i in range(100):<\/p>\n<p>    thread = threading.Thread(target=io_bound_task)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(&quot;Time taken: &quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u591a\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u4e00\u4e2a\u7b80\u5355\u7684I\/O\u4efb\u52a1\u3002\u867d\u7136\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u4e0d\u63a8\u8350\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u4f46\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u5374\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5e76\u884c\u8ba1\u7b97\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u5e93\u5982NumPy\u548cDask\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528CPU\u8d44\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import time<\/p>\n<p>def numpy_task():<\/p>\n<p>    a = np.random.rand(10000, 10000)<\/p>\n<p>    b = np.random.rand(10000, 10000)<\/p>\n<p>    c = np.dot(a, b)<\/p>\n<p>start_time = time.time()<\/p>\n<p>numpy_task()<\/p>\n<p>print(&quot;Time taken: &quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528Dask\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>Dask\u662f\u4e00\u4e2a\u7075\u6d3b\u7684\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u5927\u89c4\u6a21\u6570\u636e\u7684\u5206\u5e03\u5f0f\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.array as da<\/p>\n<p>import time<\/p>\n<p>def dask_task():<\/p>\n<p>    a = da.random.random((10000, 10000), chunks=(1000, 1000))<\/p>\n<p>    b = da.random.random((10000, 10000), chunks=(1000, 1000))<\/p>\n<p>    c = da.dot(a, b).compute()<\/p>\n<p>start_time = time.time()<\/p>\n<p>dask_task()<\/p>\n<p>print(&quot;Time taken: &quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u4f18\u5316\u5efa\u8bae<\/p>\n<\/p>\n<p><p>1\u3001\u76d1\u63a7CPU\u4f7f\u7528\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u6267\u884c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u76d1\u63a7\u5de5\u5177\uff08\u5982top\u3001htop\uff09\u6765\u5b9e\u65f6\u67e5\u770bCPU\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684I\/O\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u65f6\uff0c\u5e94\u5c3d\u91cf\u51cf\u5c11I\/O\u64cd\u4f5c\uff0c\u56e0\u4e3aI\/O\u64cd\u4f5c\u4f1a\u963b\u585eCPU\uff0c\u964d\u4f4e\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>3\u3001\u8c03\u4f18\u4ee3\u7801\u548c\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4f18\u5316\u4ee3\u7801\u548c\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3001\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><p>4\u3001\u7406\u89e3\u4efb\u52a1\u7279\u6027<\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u4efb\u52a1\u5bf9CPU\u548c\u5185\u5b58\u7684\u9700\u6c42\u4e0d\u540c\u3002\u5728\u9009\u62e9\u5e76\u884c\u8ba1\u7b97\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u9009\u62e9\u6700\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u548c\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u7a0b\u5e8f\u8dd1\u6ee1CPU\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u4efb\u52a1\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u76d1\u63a7\u548c\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884cCPU\u6027\u80fd\u6d4b\u8bd5\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6765\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u3002\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u751f\u6210\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u5176\u5e76\u884c\u6267\u884c\uff0c\u4ece\u800c\u6709\u6548\u5730\u8dd1\u6ee1CPU\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u8ba1\u7b97\u5927\u91cf\u7684\u7d20\u6570\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc4\u4f30CPU\u7684\u6027\u80fd\u548c\u8d1f\u8f7d\u80fd\u529b\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u9ad8\u6548\u5229\u7528CPU\uff1f<\/strong><br \/>\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u5229\u7528CPU\u8d44\u6e90\u3002\u5176\u4e2d\uff0c<code>concurrent.futures<\/code>\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\uff0c\u9002\u5408\u4e8eIO\u5bc6\u96c6\u578b\u6216CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u6b64\u5916\uff0c<code>Dask<\/code>\u548c<code>Joblib<\/code>\u7b49\u5e93\u4e5f\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5e76\u884c\u8ba1\u7b97\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u76d1\u63a7CPU\u4f7f\u7528\u7387\uff1f<\/strong><br \/>\u4f7f\u7528<code>psutil<\/code>\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u76d1\u63a7\u7cfb\u7edf\u7684CPU\u4f7f\u7528\u7387\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u83b7\u53d6CPU\u7684\u8d1f\u8f7d\u3001\u9891\u7387\u3001\u6e29\u5ea6\u7b49\u4fe1\u606f\u3002\u60a8\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u5b9a\u671f\u91c7\u96c6CPU\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u6839\u636e\u9700\u6c42\u5bf9\u6027\u80fd\u8fdb\u884c\u8c03\u4f18\u3002\u8fd9\u6709\u52a9\u4e8e\u60a8\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u548c\u8d44\u6e90\u6d88\u8017\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u8ba9\u7a0b\u5e8f\u8dd1\u6ee1CPU\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u5e93\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u591a\u8fdb\u7a0b\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838 [&hellip;]","protected":false},"author":3,"featured_media":981033,"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\/981026"}],"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=981026"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981026\/revisions"}],"predecessor-version":[{"id":981034,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981026\/revisions\/981034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981033"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}