《python》用psutil获取电脑CPU内存等参数信息

介绍

本人基于win10测试,本文展示了使用 Python 的 psutil 库来监听电脑的内存、CPU、GPU 等信息。

下面是一个简单的例子,展示了如何使用 psutil 库来获取当前电脑的 CPU 使用率和内存使用情况:

import psutil

# 获取 CPU 使用率
cpu_percent = psutil.cpu_percent()
print(f'CPU使用率: {
      
      cpu_percent}%')

# 获取内存使用情况
memory = psutil.virtual_memory()
total = memory.total / 1024 ** 2  # 单位转换为 MB
used = memory.used / 1024 ** 2
free = memory.free / 1024 ** 2
print(f'内存使用情况: 共{
      
      total:.2f} MB, 已使用{
      
      used:.2f} MB, 空闲{
      
      free:.2f} MB')

你还可以使用 psutil.sensors_temperatures() 方法来获取电脑的温度信息。

如果你想要监听 GPU 的运行情况,可以使用 nvidia-smi 命令行工具。你可以使用 Python 的 subprocess 模块来调用命令行工具,然后解析输出结果来获取 GPU 的信息。

下面是一个例子,展示了如何使用 subprocess 模块来调用 nvidia-smi 命令行工具并解析输出结果:

import subprocess

# 调用 nvidia-smi 命令行工具
output = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE).stdout.decode('utf-8')

# 解析输出结果
for line in output.split('\n'):
    if 'GPU' in line:
        # 解析 GPU 信息
        gpu_info = line.strip().split()
        gpu_id = gpu_info[1]
       

如果想获取信息,就需要用到另外一个库了, psutil 库中的 sensors_temperatures 方法并不在 Windows 系统上支持。

在 Windows 系统上,你可以使用 Python 的 wmi 库来获取电脑的温度信息。

下面是一个例子,展示了如何使用 wmi 库来获取电脑的 CPU 温度信息:

import wmi

# 创建 WMI 连接
conn = wmi.WMI()

# 查询 CPU 温度信息
for cpu in conn.Win32_PerfFormattedData_Counters_ThermalZoneTemperature():
    print(f'CPU温度: {
      
      cpu.CurrentTemperature}℃')

注意:这个例子只能获取 CPU 的温度信息,如果你想获取其他类型的温度信息(如硬盘、显卡等),可能需要使用其他 WMI 类。

你还可以使用其他方法来获取电脑的温度信息,比如使用命令行工具(如 sensors.exe)或者直接读取硬件传感器的值。

你可以使用 Python 的 wmi 库来获取 Windows 系统上电脑的许多信息,包括 CPU 使用率、内存使用情况、磁盘使用情况、网络流量、进程信息、电池电量等。

扫描二维码关注公众号,回复: 14658690 查看本文章

下面是一些例子,展示了如何使用 wmi 库来获取这些信息:

import wmi

# 创建 WMI 连接
conn = wmi.WMI()

# 获取 CPU 使用率
cpu_usage = conn.Win32_PerfFormattedData_PerfOS_Processor()[0].PercentProcessorTime
print(f'CPU使用率: {
      
      cpu_usage}%')

# 获取内存使用情况
memory = conn.Win32_OperatingSystem()[0]
total = int(memory.TotalVisibleMemorySize) / 1024  # 单位转换为 MB
free = int(memory.FreePhysicalMemory) / 1024
used = total - free
print(f'内存使用情况: 共{
      
      total:.2f} MB, 已使用{
      
      used:.2f} MB, 空闲{
      
      free:.2f} MB')

# 获取磁盘使用情况
disk = conn.Win32_LogicalDisk(DriveType=3)  # DriveType=3 表示硬盘
for d in disk:
    total = int(d.Size) / 1024 / 1024 / 1024  # 单位转换为 GB
    free = int(d.FreeSpace) / 1024 / 1024 / 1024
    used = total - free
    print(f'磁盘 {
      
      d.Caption} 使用情况: 共{
      
      total:.2f} GB, 已使用{
      
      used:.2f} GB, 空闲{
      
      free:.2f} GB')

# 获取网络流量信息
network = conn.Win32_PerfFormattedData_Tcpip_NetworkInterface()
for n in network:
    print(f'网卡 {
      
      n.Name} 的下载速度: {
      
      n.BytesReceivedPerSec / 1024} KB/s')
    print(f'网卡 {
      
      n.Name} 的上传速度: {
      
      n.BytesSentPerSec / 1024} KB/s')

# 获取当前运行的进程 ID 列表
process_ids = [p.ProcessId for p in conn.Win32_Process()]
print(f'当前运行的进程 ID 列表: {
      
      process_ids}')

# 获取电池电量信息
battery = conn.Win32_Battery()[0]
print(f'电池电量: {
      
      battery.EstimatedChargeRemaining}%')

猜你喜欢

转载自blog.csdn.net/linZinan_/article/details/128475798