在Python中获取Apple Silicon性能核心的数量

问题描述 投票:0回答:1

简单的问题, 我想确定 Python 脚本中的性能核心数量(最好使用

PyInstaller
冻结 Python 应用程序,这可能会产生影响)。有一些方法可以获取 CPU/核心的数量,例如
os.cpu_count()
multiprocessing.cpu_count()
psutil.cpu_count()
(后者允许区分物理核心和虚拟核心)。然而,Apple Silicon CPU 分为性能核心和效率核心,您可以通过(例如)
sysctl hw.perflevel0.logicalcpu_max
获得性能核心,使用
sysctl hw.perflevel1.logicalcpu_max
获得效率核心。除了运行
sysctl
并获取 shell 输出之外,还有什么方法可以在 Python 中获取此内容吗?

python cpu apple-silicon sysctl
1个回答
0
投票

你可以尝试这个,但我无法测试它,因为我没有使用 Mac:

import subprocess

def get_apple_silicon_performance_core_count():
    try:
        result = subprocess.run(["sysctl", "hw.perflevel0.logicalcpu_max"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
        return int(result.stdout.strip())
    except subprocess.CalledProcessError:
        # Handle the error if sysctl command fails
        return None

performance_core_count = get_apple_silicon_performance_core_count()
if performance_core_count is not None:
    print(f"Number of performance cores: {performance_core_count}")
else:
    print("Unable to determine the number of performance cores.")

--CHATGPT 的所有结果--

© www.soinside.com 2019 - 2024. All rights reserved.