【问题标题】:Get current frequency in python with psutil (Windows)使用 psutil (Windows) 在 python 中获取当前频率
【发布时间】:2021-09-13 00:29:51
【问题描述】:

我尝试使用 psutil 函数 psutil.cpu_freq(percpu=False) 获取处理器的当前频率。 但正如文档“在 Linux 上当前频率报告实时值,在所有其他平台上它代表名义上的“固定”值”中所写的那样。 有没有办法在 Windows 中获取当前频率? 谢谢!

【问题讨论】:

    标签: python windows frequency processor psutil


    【解决方案1】:

    搜索“python 窗口获取实际频率”会产生一个 Stack Overflow 问题:Unable to get current CPU frequency in Powershell or Python。它说:

    要查找当前处理器频率,您必须使用% Processor Performance 性能计数器

    所以搜索“python windows get performance counter”会产生这个 PyPI 库:winstats。它可以让你做到:

    usage = winstats.get_perf_data(r'\Processor(_Total)\% Processor Time',
                                   fmts='double', delay=100)
    print('    CPU Usage: %.02f %%' % usage)
    

    get_perf_data 的第一个参数被命名为 counters,正如文档所说,必须是 Localized Windows PerfMon counter name, or list of.。因为它必须是本地化名称,而且我手头没有英文 Windows,所以我无法重现它,但它可以工作。

    如果您希望您的代码适用于任何语言,有很多关于 Stack Overflow 的问题,这里有一个看起来很有希望的问题:https://stackoverflow.com/a/16364388/11384184

    【讨论】:

      【解决方案2】:

      @Lenormju 是对的,尽管我发现 fmt 参数需要 s。可能是最近对文档的更改,我不确定。

      这对我有用

      usage = winstats.get_perf_data(r'\Processor(_Total)\% Processor Time',
                                     fmts='double', delay=100)
      print('    CPU Usage: %.02f %%' % usage)
      

      【讨论】:

      • 确实,我刚刚检查过,你是对的。我也更新了答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2021-12-02
      • 2019-02-12
      • 1970-01-01
      相关资源
      最近更新 更多