如何在python中获得CPU利用率?

2024-09-27 21:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我使用psutil获取RAM百分比

print ('                                                                   ')
print ('----------------------CPU Information summary----------------------')
print ('                                                                   ')

# gives a single float value
vcc=psutil.cpu_count()
print ('Total number of CPUs :',vcc)

vcpu=psutil.cpu_percent()
print ('Total CPUs utilized percentage :',vcpu,'%')

但是当我运行代码时,我得到了这个

----------------------CPU Information summary----------------------

Total number of CPUs : 8

Total CPUs utilized percentage : 0.0 %

但我希望它在任务管理器中打印CPU%


Tags: ofnumberinformationcpusummarypsutilramtotal
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:17

如文件所述:

psutil.cpu\u百分比(间隔=无,percpu=假)

Return a float representing the current system-wide CPU utilization as a percentage. When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking). When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore. In this case it is recommended for accuracy that this function be called with at least 0.1 seconds between calls. When percpu is True returns a list of floats representing the utilization as a percentage for each CPU. First element of the list refers to first CPU, second element to second CPU and so on. The order of the list is consistent across calls.

因此,它需要在调用之间有一个间隔,这样它才能给出正确的百分比

这样调用它(或者尝试使用interval参数):

psutil.cpu_percent(interval=2)

相关问题 更多 >

    热门问题