{"id":1019150,"date":"2024-12-27T12:51:42","date_gmt":"2024-12-27T04:51:42","guid":{"rendered":""},"modified":"2024-12-27T12:51:59","modified_gmt":"2024-12-27T04:51:59","slug":"python%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6cpu%e6%a0%b8%e5%bf%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019150.html","title":{"rendered":"python\u5982\u4f55\u63a7\u5236cpu\u6838\u5fc3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161719\/1a6b433d-7352-4427-94a3-e1b7a9b20a3c.webp\" alt=\"python\u5982\u4f55\u63a7\u5236cpu\u6838\u5fc3\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u63a7\u5236CPU\u6838\u5fc3\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<strong>\u5229\u7528Python\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5e93\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u9650\u5236CPU\u4f7f\u7528\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u9ad8\u7ea7\u63a7\u5236<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5229\u7528Python\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5e93<\/strong>\u662f\u6bd4\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7<code>concurrent.futures<\/code>\u6a21\u5757\u7684<code>ProcessPoolExecutor<\/code>\u7c7b\u6216<code>multiprocessing<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u7a0b\u5e8f\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528<code>concurrent.futures<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u5f02\u6b65\u6267\u884c\u51fd\u6570\u8c03\u7528\u3002\u7279\u522b\u662f<code>ProcessPoolExecutor<\/code>\u7c7b\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u8fdb\u7a0b\u6c60\u7684\u5927\u5c0f\uff0c\u4ece\u800c\u63a7\u5236\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528<code>ProcessPoolExecutor<\/code><\/h4>\n<\/p>\n<p><p><code>ProcessPoolExecutor<\/code>\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002\u901a\u8fc7\u6307\u5b9a<code>max_workers<\/code>\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u8fdb\u7a0b\u6c60\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ProcessPoolExecutor<\/p>\n<p>import os<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;Running task {n} on process id {os.getpid()}&quot;)<\/p>\n<p>    return n * n<\/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>    with ProcessPoolExecutor(max_workers=4) as executor:<\/p>\n<p>        results = executor.map(task, range(10))<\/p>\n<p>        for result in results:<\/p>\n<p>            print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>max_workers=4<\/code>\u9650\u5236\u4e86\u6700\u591a\u540c\u65f6\u4f7f\u75284\u4e2aCPU\u6838\u5fc3\u3002<code>executor.map()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u4efb\u52a1\u5206\u53d1\u7ed9\u5404\u4e2a\u8fdb\u7a0b\u5e76\u884c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f18\u52bf\u4e0e\u5c40\u9650<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>ProcessPoolExecutor<\/code>\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u9700\u8981\u5e76\u884c\u8ba1\u7b97\u7684\u573a\u666f\u3002\u7136\u800c\uff0c\u5b83\u7684\u5c40\u9650\u5728\u4e8ePython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u4f1a\u9650\u5236\u7ebf\u7a0b\u7684\u5e76\u884c\u6027\uff0c\u56e0\u6b64\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u800c\u975e\u591a\u7ebf\u7a0b\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4f4e\u7ea7\u522b\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u8fdb\u7a0b\u5e76\u6307\u5b9a\u5176\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>multiprocessing<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u8fdb\u7a0b\u5e76\u6307\u5b9a\u6bcf\u4e2a\u8fdb\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u3002\u901a\u8fc7<code>cpu_count()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u7684CPU\u6838\u5fc3\u6570\u91cf\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u5206\u914d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Process, cpu_count<\/p>\n<p>def worker(num):<\/p>\n<p>    print(f&#39;Worker: {num}&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    num_cores = cpu_count()<\/p>\n<p>    print(f&quot;Number of CPU cores: {num_cores}&quot;)<\/p>\n<p>    processes = []<\/p>\n<p>    for i in range(num_cores):<\/p>\n<p>        p = Process(target=worker, args=(i,))<\/p>\n<p>        processes.append(p)<\/p>\n<p>        p.start()<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e0eCPU\u6838\u5fc3\u6570\u91cf\u76f8\u540c\u7684\u8fdb\u7a0b\u6570\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6267\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u8fdb\u9636\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86<code>Pool<\/code>\u7c7b\uff0c\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u6c60\u5e76\u901a\u8fc7<code>apply_async()<\/code>\u6216<code>map()<\/code>\u65b9\u6cd5\u5206\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    with Pool(processes=4) as pool:<\/p>\n<p>        results = pool.map(square, range(10))<\/p>\n<p>        print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>Pool<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b4\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6c60\uff0c\u5e76\u901a\u8fc7<code>map()<\/code>\u65b9\u6cd5\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u8fd9\u4e9b\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u9650\u5236CPU\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u9650\u5236Python\u7a0b\u5e8f\u4f7f\u7528\u7684CPU\u6838\u5fc3\u6570\u91cf\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>OMP_NUM_THREADS<\/code>\u6216<code>MKL_NUM_THREADS<\/code>\u7b49\u73af\u5883\u53d8\u91cf\u5b9e\u73b0\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u6570\u503c\u8ba1\u7b97\u5e93\uff08\u5982NumPy\u3001SciPy\uff09\u65f6\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u4e4b\u524d\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export OMP_NUM_THREADS=4<\/p>\n<p>export MKL_NUM_THREADS=4<\/p>\n<p>python your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u4f1a\u544a\u8bc9\u5e95\u5c42\u7684\u5e76\u884c\u8ba1\u7b97\u5e93\u9650\u5236\u4f7f\u7528\u7684\u7ebf\u7a0b\u6570\uff0c\u4ece\u800c\u95f4\u63a5\u63a7\u5236CPU\u6838\u5fc3\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f18\u52bf\u4e0e\u5c40\u9650<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u9650\u5236CPU\u4f7f\u7528\u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5355\u6709\u6548\uff0c\u7279\u522b\u9002\u5408\u4e8e\u4f7f\u7528\u6570\u503c\u8ba1\u7b97\u5e93\u7684\u573a\u666f\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u5c40\u9650\u5728\u4e8e\u5b83\u53ea\u80fd\u63a7\u5236\u7279\u5b9a\u5e93\u7684\u5e76\u884c\u884c\u4e3a\uff0c\u800c\u65e0\u6cd5\u76f4\u63a5\u63a7\u5236Python\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u9ad8\u7ea7\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u7ea7\u63a7\u5236\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>psutil<\/code>\u6216<code>numba<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528<code>psutil<\/code>\u9650\u5236CPU\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>psutil<\/code>\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u7cfb\u7edf\u786c\u4ef6\uff08\u5982CPU\u3001\u5185\u5b58\uff09\u8fdb\u884c\u76d1\u63a7\u7684\u529f\u80fd\u3002\u901a\u8fc7<code>psutil<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u8fdb\u884c\u9650\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>def limit_cpu_usage():<\/p>\n<p>    p = psutil.Process()<\/p>\n<p>    p.cpu_affinity([0, 1])  # \u9650\u5236\u8fdb\u7a0b\u4f7f\u7528\u7684CPU\u6838\u5fc3\u4e3a0\u548c1<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    limit_cpu_usage()<\/p>\n<p>    # \u5176\u4ed6\u8ba1\u7b97\u4efb\u52a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>cpu_affinity()<\/code>\u65b9\u6cd5\u9650\u5236\u5f53\u524d\u8fdb\u7a0b\u4f7f\u7528\u7279\u5b9a\u7684CPU\u6838\u5fc3\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528<code>numba<\/code>\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p><code>numba<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u52a0\u901f\u6570\u503c\u8ba1\u7b97\u7684Python\u7f16\u8bd1\u5668\u3002\u901a\u8fc7<code>numba<\/code>\u7684<code>@jit<\/code>\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from numba import jit, prange<\/p>\n<p>@jit(nopython=True, parallel=True)<\/p>\n<p>def parallel_sum(arr):<\/p>\n<p>    sum = 0<\/p>\n<p>    for i in prange(len(arr)):<\/p>\n<p>        sum += arr[i]<\/p>\n<p>    return sum<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    arr = [i for i in range(1000000)]<\/p>\n<p>    result = parallel_sum(arr)<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>parallel=True<\/code>\u53c2\u6570\u4f7f\u5f97<code>numba<\/code>\u5c1d\u8bd5\u5e76\u884c\u6267\u884c\u5faa\u73af\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63a7\u5236CPU\u6838\u5fc3\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\uff1a<strong>\u5bf9\u4e8e\u7b80\u5355\u7684\u5e76\u884c\u4efb\u52a1\uff0c\u4f7f\u7528<code>concurrent.futures<\/code>\u6216<code>multiprocessing<\/code>\u6a21\u5757\u5373\u53ef\uff1b\u82e5\u9700\u8981\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u9650\u5236CPU\u4f7f\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8fdb\u884c\u8bbe\u7f6e\uff1b\u800c\u5bf9\u4e8e\u66f4\u9ad8\u7ea7\u7684\u63a7\u5236\u9700\u6c42\uff0c\u53ef\u4ee5\u501f\u52a9<code>psutil<\/code>\u6216<code>numba<\/code>\u7b49\u7b2c\u4e09\u65b9\u5e93<\/strong>\u3002\u6839\u636e\u4efb\u52a1\u7684\u6027\u8d28\u548c\u590d\u6742\u6027\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5408\u7406\u5730\u63a7\u5236CPU\u6838\u5fc3\u7684\u4f7f\u7528\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u8d44\u6e90\u7684\u6d6a\u8d39\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u4efb\u52a1\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5e76\u884c\u6570\u503c\u8ba1\u7b97\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u73b0\u76ee\u6807\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6765\u7ba1\u7406CPU\u6838\u5fc3\u7684\u4f7f\u7528\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528<code>os<\/code>\u548c<code>psutil<\/code>\u5e93\u6765\u7ba1\u7406\u548c\u76d1\u63a7CPU\u6838\u5fc3\u7684\u4f7f\u7528\u60c5\u51b5\u3002<code>psutil<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7cfb\u7edf\u548c\u8fdb\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6CPU\u6838\u5fc3\u7684\u5229\u7528\u7387\uff0c\u5e76\u8fdb\u884c\u4e00\u4e9b\u63a7\u5236\uff0c\u6bd4\u5982\u8bbe\u7f6e\u8fdb\u7a0b\u7684CPU\u4eb2\u548c\u6027\uff0c\u9650\u5236\u67d0\u4e2a\u8fdb\u7a0b\u4f7f\u7528\u7684CPU\u6838\u5fc3\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u4f18\u5316\u7a0b\u5e8f\u4ee5\u51cf\u5c11CPU\u6838\u5fc3\u7684\u5360\u7528\uff1f<\/strong><br \/>\u4f18\u5316\u7a0b\u5e8f\u53ef\u4ee5\u4ece\u591a\u4e2a\u65b9\u9762\u5165\u624b\uff0c\u4f8b\u5982\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6765\u5145\u5206\u5229\u7528\u591a\u6838CPU\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u548c\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u5229\u7528\u5185\u7f6e\u7684<code>functools<\/code>\u6a21\u5757\u4e2d\u7684<code>lru_cache<\/code>\u6765\u7f13\u5b58\u7ed3\u679c\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u6765\u63d0\u5347\u6548\u7387\uff0c\u51cf\u5c11CPU\u8d1f\u8f7d\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u76d1\u63a7CPU\u6838\u5fc3\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>psutil<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u76d1\u63a7\u7cfb\u7edf\u7684CPU\u4f7f\u7528\u60c5\u51b5\uff0c\u5305\u62ec\u6bcf\u4e2a\u6838\u5fc3\u7684\u5229\u7528\u7387\u3001\u6e29\u5ea6\u548c\u8d1f\u8f7d\u7b49\u4fe1\u606f\u3002\u6b64\u5916\uff0c<code>GPUtil<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u6765\u76d1\u63a7GPU\u7684\u6027\u80fd\uff0c\u5982\u679c\u4f60\u7684\u5e94\u7528\u6d89\u53ca\u5230\u56fe\u5f62\u5904\u7406\u6216\u6df1\u5ea6\u5b66\u4e60\uff0c\u7ed3\u5408\u8fd9\u4e24\u4e2a\u5e93\u5c06\u63d0\u4f9b\u66f4\u5168\u9762\u7684\u6027\u80fd\u76d1\u63a7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u63a7\u5236CPU\u6838\u5fc3\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u5229\u7528Python\u7684\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5e93\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u9650\u5236C [&hellip;]","protected":false},"author":3,"featured_media":1019172,"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\/1019150"}],"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=1019150"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019150\/revisions"}],"predecessor-version":[{"id":1019174,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019150\/revisions\/1019174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019172"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}