如何在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个回答
-4
投票

你可以尝试这个,但我无法测试它,因为我没有使用 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.