本文共 3697 字,大约阅读时间需要 12 分钟。
OK, here’s another psutil release. Main highlights of this release are sensors-related APIs.
好,这是另一个psutil版本。 此版本的主要亮点是与传感器相关的API。
It is now possible to retrieve hardware temperatures. The relevant commit is . Unfortunately this is Linux only. I couldn’t manage to implement this on other platforms mainly for two reasons:
现在可以检索硬件温度。 相关的提交在 。 不幸的是,这仅是Linux。 我无法在其他平台上实现此功能主要有两个原因:
>>> import psutil>>> psutil.sensors_temperatures(){'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)], 'asus': [shwtemp(label='', current=47.0, high=None, critical=None)], 'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0), shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0), shwtemp(label='Core 1', current=52.0, high=100.0, critical=100.0), shwtemp(label='Core 2', current=45.0, high=100.0, critical=100.0), shwtemp(label='Core 3', current=47.0, high=100.0, critical=100.0)]}
This works on Linux, Windows and FreeBSD and provides battery status information. The relevant commit is .
它可以在Linux,Windows和FreeBSD上运行,并提供电池状态信息。 相关的提交在 。
>>> import psutil>>>>>> def secs2hours(secs):... mm, ss = divmod(secs, 60)... hh, mm = divmod(mm, 60)... return "%d:%02d:%02d" % (hh, mm, ss)...>>> battery = psutil.sensors_battery()>>> batterysbattery(percent=93, secsleft=16628, power_plugged=False)>>> print("charge = %s%%, time left = %s" % (batt.percent, secs2hours(batt.secsleft)))charge = 93%, time left = 4:37:08
Available under Linux, Windows and OSX. Relevant commit is . Linux is the only platform which reports the real-time value (always changing), on all other platforms current frequency is represented as the nominal “fixed” value.
在Linux,Windows和OSX下可用。 相关的提交在 。 Linux是唯一报告实时值(始终在变化)的平台,在所有其他平台上,当前频率表示为标称“固定”值。
>>> import psutil>>> psutil.cpu_freq()scpufreq(current=931.42925, min=800.0, max=3500.0)>>> psutil.cpu_freq(percpu=True)[scpufreq(current=2394.945, min=800.0, max=3500.0), scpufreq(current=2236.812, min=800.0, max=3500.0), scpufreq(current=1703.609, min=800.0, max=3500.0), scpufreq(current=1754.289, min=800.0, max=3500.0)]
This will let you know what CPU number a process is currently running on, which is somewhat related to the existent functionality. The relevant commit is . It is interesting to use this method to visualize how the OS scheduler continuously evenly reassigns processes to different CPUs (see script).
这将让您知道进程当前正在运行的CPU号,这与现有的功能有些相关。 相关的提交在 。 使用此方法来可视化OS调度程序如何将进程连续均匀地重新分配给不同的CPU(请参阅脚本)是很有趣的。
Process().cpu_affinity([])
…can now be used as an alias for “set affinity against all eligible CPUs”. This was implemented because it turns out it is not always possible to set affinity against all CPUs. Having such an alias is also a shortcut to avoid doing this, which is kinda verbose:
…现在可以用作“设置对所有合格CPU的亲和力”的别名。 之所以实现此功能,是因为事实证明, ,并非总是可能对所有CPU设置亲和力。 拥有这样的别名也是避免这样做的捷径,有点冗长:
psutil.Process().cpu_affinity(list(range(psutil.cpu_count())))
翻译自:
转载地址:http://ygqwd.baihongyu.com/