如何从系统监控程序中捕获CPU核心使用情况?

发布于 2024-11-25 11:28:19 字数 178 浏览 0 评论 0原文

我有一个使用并行 python 运行的 python 程序。该模块将我提交给计算机各个核心的作业进行扩展,我不知道它是如何做到的,但当我检查系统监视器时,它清楚地显示两个核心的使用率均为 100%(我正在运行一些非常繁重的作业)。

是否有任何 python 模块或工具可以让我在运行作业时从系统监控程序捕获各个核心的使用情况?

I have a python program running using a parallel python. This module scales my job submitted to individual cores of my computer , I don't know how does it do it but when i check my system monitor it clearly shows 100% usage in both the cores (I am running some really heavy jobs).

Is there any python module or tool which allows me to capture the individual core usage from the system monitor program when I run my job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

季末如歌 2024-12-02 11:28:19

psutil

>>> for x in range(3):
...     psutil.cpu_percent(interval=1, percpu=True)
... 
[4.0, 6.9]
[7.0, 8.5]
[1.2, 9.0]

psutil:

>>> for x in range(3):
...     psutil.cpu_percent(interval=1, percpu=True)
... 
[4.0, 6.9]
[7.0, 8.5]
[1.2, 9.0]
笑忘罢 2024-12-02 11:28:19
import psutil 

values = psutil.cpu_percent(percpu=True) 
print values 

[0.0, 0.0, 0.0] 

values = psutil.cpu_percent() 
print values 

0.0
import psutil 

values = psutil.cpu_percent(percpu=True) 
print values 

[0.0, 0.0, 0.0] 

values = psutil.cpu_percent() 
print values 

0.0
如果没有 2024-12-02 11:28:19

因此,如果您有办法调用系统监视器将核心使用情况写入标准输出,您始终可以执行以下操作:

import subprocess
command = 'top -b -n 1'  # or whatever you use
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate[0]

# parse output here and extract cpu usage. this is super-dependent
# on the layout of your system monitor output
cpuline = output.split('\n')[2]

您可以阅读 subprocess 的工作原理 此处

So if you have a way to call the system monitor to write the core usage to stdout, you can always do something like this:

import subprocess
command = 'top -b -n 1'  # or whatever you use
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate[0]

# parse output here and extract cpu usage. this is super-dependent
# on the layout of your system monitor output
cpuline = output.split('\n')[2]

You can read up on how subprocess works here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文