{"id":1039111,"date":"2024-12-31T12:29:46","date_gmt":"2024-12-31T04:29:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039111.html"},"modified":"2024-12-31T12:29:48","modified_gmt":"2024-12-31T04:29:48","slug":"%e5%a6%82%e4%bd%95%e9%99%90%e5%88%b6python%e7%9a%84cpu%e4%bd%bf%e7%94%a8%e7%8e%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039111.html","title":{"rendered":"\u5982\u4f55\u9650\u5236python\u7684cpu\u4f7f\u7528\u7387"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ca84853e-274e-40cc-9ce6-a51ffda1ec03.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u9650\u5236python\u7684cpu\u4f7f\u7528\u7387\" \/><\/p>\n<p><p> <strong>\u8981\u9650\u5236Python\u7684CPU\u4f7f\u7528\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\u3001\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177\u6765\u9650\u5236\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528 <code>psutil<\/code> \u5e93\u6765\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\u662f\u6bd4\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\u3002<code>psutil<\/code> \u5e93\u4e0d\u4ec5\u53ef\u4ee5\u9650\u5236CPU\u4f7f\u7528\u7387\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u76f8\u5173\u7684\u4fe1\u606f\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>psutil<\/code> \u5e93\u6765\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>psutil.Process().cpu_affinity()<\/code> \u65b9\u6cd5\u5c06\u8fdb\u7a0b\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684CPU\u6838\u5fc3\uff0c\u4ece\u800c\u9650\u5236\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5 <code>psutil<\/code> \u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install psutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528 <code>psutil<\/code> \u5e93\u6765\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b<\/strong><\/h2>\n<p>p = psutil.Process(os.getpid())<\/p>\n<h2><strong>\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\uff0c\u4f8b\u5982\u7ed1\u5b9a\u5230CPU 0\u548c1<\/strong><\/h2>\n<p>p.cpu_affinity([0, 1])<\/p>\n<h2><strong>\u8fd0\u884cCPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5c06\u5f53\u524d\u8fdb\u7a0b\u7ed1\u5b9a\u5230CPU 0\u548c1\uff0c\u4ece\u800c\u9650\u5236\u5176\u53ef\u4ee5\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h2>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u53ef\u4ee5\u7528\u4e8e\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u7531\u4e8ePython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cPython\u5b57\u8282\u7801\u3002\u56e0\u6b64\uff0c\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u53ef\u4ee5\u5e2e\u52a9\u9650\u5236CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def cpu_intensive_task():<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=cpu_intensive_task)<\/p>\n<p>thread.start()<\/p>\n<h2><strong>\u9650\u5236\u4e3b\u7ebf\u7a0b\u7684CPU\u4f7f\u7528\u7387<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    time.sleep(0.1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f46\u4e0d\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u591a\u8fdb\u7a0b<\/h2>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u53ef\u4ee5\u66f4\u597d\u5730\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684GIL\uff0c\u56e0\u6b64\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u6765\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u5e76\u4f7f\u7528<code>time.sleep()<\/code>\u6765\u9650\u5236\u6bcf\u4e2a\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def cpu_intensive_task():<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(0.1)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b<\/strong><\/h2>\n<p>processes = []<\/p>\n<p>for _ in range(4):<\/p>\n<p>    p = multiprocessing.Process(target=cpu_intensive_task)<\/p>\n<p>    p.start()<\/p>\n<p>    processes.append(p)<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for p in processes:<\/p>\n<p>    p.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5e76\u4e14\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177<\/h2>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cpulimit<\/code>\u5de5\u5177\u6765\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002<code>cpulimit<\/code>\u5de5\u5177\u53ef\u4ee5\u9650\u5236\u7279\u5b9a\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5<code>cpulimit<\/code>\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install cpulimit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>cpulimit<\/code>\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">cpulimit -e python3 -l 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u4e3a50%\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>psutil<\/code>\u5e93<\/h2>\n<\/p>\n<p><p><code>psutil<\/code>\u5e93\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5e76\u53ef\u4ee5\u7528\u6765\u9650\u5236\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b<\/strong><\/h2>\n<p>p = psutil.Process(os.getpid())<\/p>\n<h2><strong>\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\uff0c\u4f8b\u5982\u7ed1\u5b9a\u5230CPU 0\u548c1<\/strong><\/h2>\n<p>p.cpu_affinity([0, 1])<\/p>\n<h2><strong>\u8fd0\u884cCPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u9650\u5236\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528<code>cgroups<\/code>\uff08Linux\uff09<\/h2>\n<\/p>\n<p><p><code>cgroups<\/code>\uff08\u63a7\u5236\u7ec4\uff09\u662fLinux\u5185\u6838\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u7528\u4e8e\u9650\u5236\u3001\u8bb0\u5f55\u548c\u9694\u79bb\u8fdb\u7a0b\u7684\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528<code>cgroups<\/code>\u6765\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u5e76\u914d\u7f6e<code>cgroups<\/code>\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo cgcreate -g cpu:\/cpulimited<\/p>\n<p>echo 50000 | sudo tee \/sys\/fs\/cgroup\/cpu\/cpulimited\/cpu.cfs_quota_us<\/p>\n<p>echo $$ | sudo tee \/sys\/fs\/cgroup\/cpu\/cpulimited\/tasks<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>cpulimited<\/code>\u7684<code>cgroups<\/code>\uff0c\u5e76\u5c06\u5f53\u524dShell\u8fdb\u7a0b\u6dfb\u52a0\u5230\u6b64<code>cgroups<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u5728\u9650\u5236\u4e0b\u8fd0\u884cPython\u7a0b\u5e8f\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u591a\u4e2a\u8fdb\u7a0b\u8fdb\u884c\u7edf\u4e00\u7ba1\u7406\u548c\u9650\u5236\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528<code>nice<\/code>\u548c<code>ionice<\/code>\u547d\u4ee4<\/h2>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>nice<\/code>\u548c<code>ionice<\/code>\u547d\u4ee4\u6765\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u4ece\u800c\u95f4\u63a5\u9650\u5236CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>nice<\/code>\u547d\u4ee4\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">nice -n 10 python3 your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u4ee5\u8f83\u4f4e\u7684\u4f18\u5148\u7ea7\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u4ece\u800c\u51cf\u5c11\u5176\u5bf9CPU\u7684\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>ionice<\/code>\u547d\u4ee4\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">ionice -c 3 -p $$ <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u5f53\u524dShell\u8fdb\u7a0b\u7684I\/O\u4f18\u5148\u7ea7\u8bbe\u7f6e\u4e3a\u6700\u4f4e\uff0c\u4ece\u800c\u51cf\u5c11\u5176\u5bf9I\/O\u7684\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u5408\u4f7f\u7528<code>nice<\/code>\u548c<code>ionice<\/code>\u547d\u4ee4\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">nice -n 10 ionice -c 3 python3 your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u663e\u8457\u5f71\u54cd\u7cfb\u7edf\u5176\u4ed6\u8fdb\u7a0b\u7684\u60c5\u51b5\u4e0b\uff0c\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u548cI\/O\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>ThreadPoolExecutor<\/code>\u548c<code>ProcessPoolExecutor<\/code>\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u521b\u5efa\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\uff0c\u4ece\u800c\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import time<\/p>\n<p>def cpu_intensive_task():<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(0.1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    futures = [executor.submit(cpu_intensive_task) for _ in range(4)]<\/p>\n<p>    concurrent.futures.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(futures)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b\u6c60<\/strong><\/h2>\n<p>with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    futures = [executor.submit(cpu_intensive_task) for _ in range(4)]<\/p>\n<p>    concurrent.futures.wait(futures)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u548c\u8fdb\u7a0b\uff0c\u5e76\u9650\u5236\u5b83\u4eec\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9650\u901f\u5668<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9650\u901f\u5668\u6765\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u901a\u8fc7\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u8c03\u7528<code>time.sleep()<\/code>\u6765\u964d\u4f4eCPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def rate_limited_task():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6267\u884c\u4efb\u52a1<\/p>\n<p>        time.sleep(0.1)  # \u9650\u5236CPU\u4f7f\u7528\u7387<\/p>\n<h2><strong>\u8fd0\u884c\u9650\u901f\u4efb\u52a1<\/strong><\/h2>\n<p>rate_limited_task()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u7ec6\u63a7\u5236CPU\u4f7f\u7528\u7387\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528 <code>joblib<\/code> \u5e93<\/h2>\n<\/p>\n<p><p><code>joblib<\/code> \u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5e76\u884c\u8ba1\u7b97\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>n_jobs<\/code>\u53c2\u6570\u6765\u9650\u5236CPU\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5 <code>joblib<\/code> \u5e93\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install joblib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from joblib import Parallel, delayed<\/p>\n<p>import time<\/p>\n<p>def cpu_intensive_task():<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(0.1)<\/p>\n<h2><strong>\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff0c\u9650\u5236\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf<\/strong><\/h2>\n<p>Parallel(n_jobs=2)(delayed(cpu_intensive_task)() for _ in range(4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5e76\u884c\u6267\u884c\u4efb\u52a1\u5e76\u9650\u5236CPU\u4f7f\u7528\u7387\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528Docker\u5bb9\u5668<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Docker\u5bb9\u5668\u53ef\u4ee5\u65b9\u4fbf\u5730\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u901a\u8fc7\u8bbe\u7f6eDocker\u5bb9\u5668\u7684CPU\u9650\u5236\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236Python\u7a0b\u5e8f\u7684\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efaDocker\u5bb9\u5668\u5e76\u9650\u5236CPU\u4f7f\u7528\u7387\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">docker run -d --cpus=&quot;.5&quot; python:3.8-slim python -c &quot;while True: pass&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u521b\u5efa\u4e00\u4e2a\u8fd0\u884cPython\u7a0b\u5e8f\u7684Docker\u5bb9\u5668\uff0c\u5e76\u5c06\u5176CPU\u4f7f\u7528\u7387\u9650\u5236\u4e3a50%\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u9650\u5236Python\u7684CPU\u4f7f\u7528\u7387\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\u3001\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177\u3001\u4f7f\u7528<code>psutil<\/code>\u5e93\u3001\u4f7f\u7528<code>cgroups<\/code>\u3001\u4f7f\u7528<code>nice<\/code>\u548c<code>ionice<\/code>\u547d\u4ee4\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9650\u901f\u5668\u3001\u4f7f\u7528<code>joblib<\/code>\u5e93\u4ee5\u53ca\u4f7f\u7528Docker\u5bb9\u5668\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u8d44\u6e90\u7684\u5408\u7406\u5229\u7528\u548c\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u76d1\u63a7CPU\u4f7f\u7528\u7387\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528psutil\u5e93\u6765\u76d1\u63a7CPU\u4f7f\u7528\u7387\u3002\u901a\u8fc7\u8c03\u7528<code>psutil.cpu_percent(interval=1)<\/code>\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u7684CPU\u4f7f\u7528\u7387\u3002\u5982\u679c\u5e0c\u671b\u76d1\u63a7\u7279\u5b9a\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528<code>psutil.Process(pid).cpu_percent(interval=1)<\/code>\uff0c\u5176\u4e2dpid\u662f\u76ee\u6807\u8fdb\u7a0b\u7684ID\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\u548c\u8d44\u6e90\u5360\u7528\u60c5\u51b5\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u6765\u95f4\u63a5\u9650\u5236Python\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>os.nice()<\/code>\u51fd\u6570\u6765\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u3002Linux\u7528\u6237\u5219\u53ef\u4ee5\u4f7f\u7528<code>nice<\/code>\u547d\u4ee4\u6765\u542f\u52a8Python\u7a0b\u5e8f\uff0c\u4ece\u800c\u964d\u4f4e\u5176CPU\u4f7f\u7528\u7387\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u63a7\u5236\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u7684\u6570\u91cf\u6765\u63a7\u5236\u8d44\u6e90\u5360\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u65f6\u95f4\u5ef6\u8fdf\u662f\u5426\u80fd\u6709\u6548\u964d\u4f4eCPU\u4f7f\u7528\u7387\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u65f6\u95f4\u5ef6\u8fdf\u53ef\u4ee5\u5e2e\u52a9\u964d\u4f4ePython\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u7387\u3002\u4f8b\u5982\uff0c\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0<code>time.sleep()<\/code>\u51fd\u6570\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u5728\u6267\u884c\u4efb\u52a1\u65f6\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u4ece\u800c\u51cf\u5c11\u5bf9CPU\u7684\u5360\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u6267\u884c\u4efb\u52a1\u4f46\u53c8\u4e0d\u5e0c\u671b\u5360\u7528\u8fc7\u591aCPU\u8d44\u6e90\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6570\u636e\u91c7\u96c6\u6216\u5b9a\u65f6\u4efb\u52a1\u3002\u901a\u8fc7\u8c03\u6574\u5ef6\u8fdf\u65f6\u95f4\uff0c\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236CPU\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u9650\u5236Python\u7684CPU\u4f7f\u7528\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u8bbe\u7f6eCPU\u4eb2\u548c\u6027\u3001\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177\u6765\u9650\u5236\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1039126,"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\/1039111"}],"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=1039111"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039111\/revisions"}],"predecessor-version":[{"id":1039130,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039111\/revisions\/1039130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039126"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}