在python中获取CPU温度
我需要一个在 python 中访问 CPU 温度的示例代码。
顺便说一句,我运行的是 Windows 7。
I need an example code for accessing CPU temperature in python.
I'm running windows 7, BTW.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 WMI 模块 + 打开硬件监视器 + 此处描述的 WMI 界面。
示例代码:
Use the WMI module + Open Hardware Monitor + its WMI interface described here.
Sample code:
下载 http://openhardwaremonitor.org/downloads/ 和 http://www.cputhermometer.com/ 并提取 OpenHardwareMonitorLib.dll 和 CPUThermometerLib.dll 并将它们放在一个目录中。
然后,您可以使用 pythonnet 模块来寻址 .dll 并提取这些程序提供的任何统计信息。 cputhermometer 提供每个核心的 CPU 温度,openhardwaremonitor 提供其他一切。无需使用 WMI,它要求程序在后台处于活动状态。
我编写了一个小脚本(python 3.6.5)来显示系统上可用的每个温度传感器,您当然可以轻松地将其修改为其他传感器类型。您必须以管理员身份运行此命令:
这是我系统上的输出:
有关更多文档(但是您应该能够从上面的代码推断出您需要的所有内容),请参阅 https://github.com/openhardwaremonitor/openhardwaremonitor/(或 cputhermometer,在网站上)源代码,使用它们时功能和方法是相同的Python。
我尚未在任何其他计算机上对此进行测试,因此不同的处理器架构的功能可能不相同。
确保在测量之间运行
Hardware[x].Update()
(如果需要,还可运行SubHardware[x].Update()
)。Download http://openhardwaremonitor.org/downloads/ and http://www.cputhermometer.com/ and extract OpenHardwareMonitorLib.dll and CPUThermometerLib.dll and place these in a directory.
You can then use the pythonnet module to address the .dlls and pull any stat that these programs offer. cputhermometer offers per-core CPU temps, openhardwaremonitor offers everything else. No need to use WMI which requires the program to be active in the background.
I have written a small script (python 3.6.5) to show every temperature sensor available on the system, you can of course easily modify this for other sensor types. You must run this as administrator:
Here is the output on my system:
For further documentation (however you should be able to infer everything you need from the above code), refer to the https://github.com/openhardwaremonitor/openhardwaremonitor/ (or cputhermometer, on the website) source code, the functions and methods are identical when you use these with python.
I haven't tested this on any other computers, so different processor architectures may not function identically.
Ensure you run
Hardware[x].Update()
between taking measurements (andSubHardware[x].Update()
if needed).您可以使用 pywin32 访问本机 Windows API。我相信如果主板驱动程序的制造商通过他们的驱动程序注册了 WMI 数据提供程序,应该可以查询 Windows API 来获取 CPU 温度。假设是这种情况,您可以下载 pywin32 扩展 和 Python WMI module 在ars的答案中提到,然后按如下方式进行:
查看ars的答案中的IronPython脚本似乎有也可以采用另一种方法,使用不同的 WMI 对象。使用相同的 API 和方法,您可以尝试接收温度值,
显然应该返回以十分之一开尔文为单位的温度值,因此要接收以摄氏度为单位的温度,我想您只需将该值除以 10 并减去 ~273 即可。
You can use pywin32 to access the native Windows API. I believe it should be possible to query the Windows API for the CPU temperature if the manufacturer for your mainboard driver registers a WMI Data Provider through their driver. Assuming this is the case you could download the pywin32 extensions and the Python WMI module mentioned in the answer by ars, and then proceed as follows:
Looking at the IronPython script in the ars' answer there seems to be another way to do it too, using a different WMI object. Using the same API and approach you could try receiving the temperature value with
which apparently should return the temperature value in tenths of Kelvin, thus to receive the degree in Celsius I guess you just divide this value by 10 and subtract ~273.
我从第三方收到了一个 C++ 项目,并找到了如何使用 C++ 获取 CPU 和主板温度。然后我发现 this 我用它来帮助我用 ctypes 模拟 python 中的 C++ 函数,a许多代码是直接从该存储库复制的。 “\\.\AdvLmDev”特定于我所使用的 PC,应替换为“\\.\PhysicalDrive0”。还有一个功能可以获取其他 CPU 功率测量值。我这样做是因为我不想使用开放硬件监视器。您可能必须以管理员身份运行代码才能正常工作。
I received a C++ project from a 3rd party and found how to get the CPU and Board Temp using C++. I then found this which I used to to help me mimic the C++ functions in python with ctypes, a lot of this code is copied directly from that repository. '\\.\AdvLmDev' is specific to the PC I was using, and should be replaced with '\\.\PhysicalDrive0'. There is also a function to get other CPU power measurements. I did this because I didn't want to use Open Hardware Monitor. You may have to run the code as administrator for it to work.
查看 cputemp 库。
编辑:在 Windows 上,您也许可以转换此 IronPython 脚本,使用 python WMI 库 使用 WMI 。
Check out the cputemp library.
EDIT: on windows, you might be able to convert this IronPython script which uses WMI using the python WMI library.
我已经使用了 https://github.com/BennyCarbajal/PyTherm 中的实用程序
几乎安装了 pythonnet :
然后以管理员身份执行终端,最后执行文件:
如果以普通用户身份运行它也可以,但不会输出那么多数据。
由于它以 JSON 格式返回数据,因此您应该很容易获取您正在寻找的特定硬件的数据。
I have used the utility from https://github.com/BennyCarbajal/PyTherm
Pretty much install pythonnet with:
Then just execute the terminal as administrator and finally execute the file:
It also works if you run it as a regular user but will not output as much data.
Since it returns the data in JSON format, it should be easy for you to just get the data of the specific hardware you are looking for.
eadmaster 提供的代码可能适用于 OpenHardwareMonitor 已编程的旧 CPU,但我有 Skylake i7 6700K CPU。 OpenHardwareMonitor 没有为我提供任何结果。但是,该程序有一个名为 CPU Thermometer 的分支,它基于 OpenHardwareMonitor,可以< /em> 识别我的 CPU。
在寻找如何通过 Python 获取 CPU 温度的过程中,我切换到了 IronPython,以便能够访问 .Net 框架并轻松访问其他性能数据,但应该很容易弄清楚如何将其改造为普通版本Python 2.7(只需运行 CPU Thermometer 而不是 OpenHardwareMonitor 并将命名空间更改为“root\CPUThermometer”?就这么简单吗?)。
示例输出:
我发现查询不太标准 SQL 并且不喜欢“Order By”子句,因此我必须执行一些奇特的字符串格式才能在查询反向返回核心时获得正确的顺序命令。这让我有点困惑,直到我设计了这种方法。
The code offered by eadmaster may work for older CPUs that OpenHardwareMonitor has been programmed for, but I have a Skylake i7 6700K CPU. OpenHardwareMonitor offered no results for me. However, there is a fork of this program called CPU Thermometer, which is based on OpenHardwareMonitor which does recognize my CPU.
In chasing down how to get CPU temps via Python, I switched to IronPython in order to have access to the .Net framework and have easy access to other performance data, but it should be fairly easy to figure out how to retro fit it for vanilla Python 2.7 (Just run CPU Thermometer instead ofOpenHardwareMonitor and change your namespace to "root\CPUThermometer"? Could it be that simple?).
Sample Output:
I found out the hard way that the query is not quite standard SQL and DOES NOT LIKE the "Order By" clause, so I had to do some fancy string formatting to get the order correct as the query returns the cores in reverse order. This baffled me for a bit until I devised this way around it.
对于那些尝试在 Windows 操作系统上使用 python 运行 OpenHardwareMonitor 时遇到问题的人,这两种解决方案都无法帮助我,即使我在管理员提示符下运行它(是的,我们需要以管理员权限运行它,但这对我来说还不够)。
对我来说修复的是卸载 python 并使用“所有用户”选项再次安装它(以便管理员帐户可以使用 python)。然后OpenHardwareLib.dll终于加载成功了。
For those who had your issues trying to run OpenHardwareMonitor with python on Windows OS, neither solutions helped me, even if I runned it on Admin prompt (yes we need to run it with Admin privileges, but it was not enough on my case).
What have fixed for me was to uninstall python and install it again using the "All Users" option (so that python can be used by Admin account). Then the OpenHardwareLib.dll was finally loaded successfully.