{"id":991184,"date":"2024-12-27T08:26:28","date_gmt":"2024-12-27T00:26:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/991184.html"},"modified":"2024-12-27T08:26:30","modified_gmt":"2024-12-27T00:26:30","slug":"python%e5%a6%82%e4%bd%95%e5%8d%a0%e7%94%a8cpu%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/991184.html","title":{"rendered":"python\u5982\u4f55\u5360\u7528cpu\u5185\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065603\/321d4060-3260-4a98-adc7-2507f2deab2b.webp\" alt=\"python\u5982\u4f55\u5360\u7528cpu\u5185\u5b58\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5360\u7528CPU\u548c\u5185\u5b58\u8d44\u6e90\uff0c\u5305\u62ec\u4f7f\u7528\u590d\u6742\u7b97\u6cd5\u3001\u5904\u7406\u5927\u6570\u636e\u96c6\u3001\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u590d\u6742\u7b97\u6cd5\u548c\u5904\u7406\u5927\u6570\u636e\u96c6\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u4eec\u9700\u8981\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u548c\u5185\u5b58\u6765\u5b58\u50a8\u6570\u636e\u3002\u6b64\u5916\uff0cPython\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e5f\u53ef\u4ee5\u589e\u52a0CPU\u548c\u5185\u5b58\u7684\u5360\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5176\u4e2d\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u590d\u6742\u7b97\u6cd5\u548c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u590d\u6742\u7b97\u6cd5\u548c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u901a\u5e38\u9700\u8981\u5927\u91cf\u7684CPU\u8d44\u6e90\u3002\u8fd9\u4e9b\u4efb\u52a1\u53ef\u80fd\u6d89\u53ca\u5230\u5927\u91cf\u7684\u6570\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u5bfc\u81f4\u9ad8CPU\u5360\u7528\u7684\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6df1\u5ea6\u5b66\u4e60\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b97\u6cd5<\/strong><br \/>\u4f7f\u7528Python\u7684\u6df1\u5ea6\u5b66\u4e60\u548c\u673a\u5668\u5b66\u4e60\u5e93\uff08\u5982TensorFlow\u3001PyTorch\u7b49\uff09\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u65f6\uff0c\u901a\u5e38\u4f1a\u6d88\u8017\u5927\u91cf\u7684CPU\u8d44\u6e90\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e9b\u7b97\u6cd5\u9700\u8981\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u8fed\u4ee3\u8ba1\u7b97\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u590d\u6742\u6a21\u578b\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u5f62\u548c\u56fe\u50cf\u5904\u7406<\/strong><br \/>\u56fe\u5f62\u548c\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u901a\u5e38\u9700\u8981\u5bf9\u50cf\u7d20\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u6ee4\u6ce2\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4CPU\u7684\u9ad8\u5360\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u5927\u6570\u636e\u96c6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u662fPython\u7a0b\u5e8f\u5360\u7528\u5927\u91cf\u5185\u5b58\u7684\u53e6\u4e00\u4e2a\u5e38\u89c1\u539f\u56e0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u5bfc\u81f4\u9ad8\u5185\u5b58\u5360\u7528\u7684\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c<\/strong><br \/>\u4f7f\u7528Pandas\u7b49\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u5c24\u5176\u662f\u5728\u5185\u5b58\u4e2d\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u8bfb\u53d6\u3001\u8fc7\u6ee4\u3001\u805a\u5408\u548c\u8f6c\u6362\u6570\u636e\u90fd\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u7684\u5feb\u901f\u589e\u957f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u52a0\u8f7d\u548c\u5b58\u50a8<\/strong><br \/>\u5728\u5185\u5b58\u4e2d\u52a0\u8f7d\u6574\u4e2a\u6570\u636e\u96c6\uff08\u5982CSV\u3001JSON\u6587\u4ef6\uff09\u4f1a\u5bfc\u81f4\u9ad8\u5185\u5b58\u5360\u7528\u3002\u4e3a\u4e86\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff0c\u53ef\u4ee5\u8003\u8651\u5206\u5757\u52a0\u8f7d\u6570\u636e\u6216\u4f7f\u7528\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\uff0c\u4f46\u4e5f\u53ef\u80fd\u589e\u52a0CPU\u548c\u5185\u5b58\u7684\u5360\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/strong><br \/>\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4ee5\u4fbf\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u7136\u800c\uff0c\u7531\u4e8ePython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0c\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u53ef\u80fd\u5e76\u4e0d\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u53cd\u800c\u4f1a\u589e\u52a0\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u8fdb\u7a0b\u7f16\u7a0b<\/strong><br \/>\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6765\u6267\u884c\u4efb\u52a1\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u901a\u5e38\u4f1a\u6bd4\u591a\u7ebf\u7a0b\u66f4\u6709\u6548\u5730\u5229\u7528\u591a\u6838CPU\u3002\u7136\u800c\uff0c\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u4e5f\u4f1a\u5bfc\u81f4\u66f4\u591a\u7684\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f18\u5316Python\u7a0b\u5e8f\u7684\u8d44\u6e90\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u51cf\u5c11Python\u7a0b\u5e8f\u5bf9CPU\u548c\u5185\u5b58\u7684\u5360\u7528\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u4f18\u5316\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/strong><br \/>\u9009\u62e9\u9002\u5408\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u964d\u4f4e\u8d44\u6e90\u7684\u6d88\u8017\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u548c\u96c6\u5408\u4ee3\u66ff\u5217\u8868\u6765\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/strong><br \/>\u4f7f\u7528\u751f\u6210\u5668\u4ee3\u66ff\u5217\u8868\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6570\u636e\u96c6\u5230\u5185\u5b58\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u8ba1\u7b97<\/strong><br \/>\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\uff0c\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002\u9009\u62e9\u5408\u9002\u7684\u5e76\u884c\u5316\u7b56\u7565\uff08\u5982\u6570\u636e\u5e76\u884c\u6216\u4efb\u52a1\u5e76\u884c\uff09\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5916\u90e8\u5e93\u7684\u4f7f\u7528<\/strong><br \/>\u4f7f\u7528\u9ad8\u6548\u7684\u5916\u90e8\u5e93\uff08\u5982NumPy\u3001SciPy\uff09\u6765\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u8fd9\u4e9b\u5e93\u901a\u5e38\u7528C\u8bed\u8a00\u7f16\u5199\uff0c\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11Python\u7a0b\u5e8f\u5bf9CPU\u548c\u5185\u5b58\u7684\u5360\u7528\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u76d1\u6d4bPython\u7a0b\u5e8f\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>psutil<\/code>\u5e93\u6765\u76d1\u6d4bPython\u7a0b\u5e8f\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u901a\u8fc7<code>psutil.cpu_percent()<\/code>\u53ef\u4ee5\u83b7\u5f97\u5f53\u524dCPU\u5360\u7528\u7387\uff0c\u800c<code>psutil.virtual_memory()<\/code>\u5219\u53ef\u4ee5\u83b7\u53d6\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u65f6\u4e86\u89e3\u7a0b\u5e8f\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u54ea\u4e9b\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u9ad8CPU\u548c\u5185\u5b58\u5360\u7528\uff1f<\/strong><br \/>\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u3001\u5faa\u73af\u3001\u9012\u5f52\u8c03\u7528\u3001\u4ee5\u53ca\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u90fd\u4f1a\u5bfc\u81f4CPU\u548c\u5185\u5b58\u5360\u7528\u7387\u7684\u663e\u8457\u4e0a\u5347\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4e0d\u5f53\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5927\u578b\u5217\u8868\u6216\u5b57\u5178\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u7684\u9ad8\u6d88\u8017\u3002\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u8d44\u6e90\u7684\u5360\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u4ee3\u7801\u4ee5\u51cf\u5c11CPU\u548c\u5185\u5b58\u5360\u7528\uff1f<\/strong><br \/>\u4ee3\u7801\u4f18\u5316\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u751f\u6210\u5668\u4ee3\u66ff\u5217\u8868\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5185\u5b58\u5360\u7528\uff1b\u5c06\u590d\u6742\u7684\u903b\u8f91\u5206\u89e3\u4e3a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u6b64\u5916\uff0c\u5229\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u591a\u6838CPU\uff0c\u63d0\u5347\u6267\u884c\u6548\u7387\uff0c\u4ece\u800c\u51cf\u5c11\u5355\u4e2a\u7ebf\u7a0b\u7684CPU\u5360\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5360\u7528CPU\u548c\u5185\u5b58\u8d44\u6e90\uff0c\u5305\u62ec\u4f7f\u7528\u590d\u6742\u7b97\u6cd5\u3001\u5904\u7406\u5927\u6570\u636e\u96c6\u3001\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a [&hellip;]","protected":false},"author":3,"featured_media":991190,"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\/991184"}],"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=991184"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991184\/revisions"}],"predecessor-version":[{"id":991192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991184\/revisions\/991192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/991190"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=991184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=991184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=991184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}