如何在Python中获取特定程序的当前CPU,GPU和RAM使用情况?

时间:2018-06-17 14:56:03

标签: python linux macos psutil

我已经编写了一个python程序来使用Haar Cascade检测视频输入(网络摄像头)的面部。我想知道这个特定程序使用了多少CPU,GPU和RAM,而不是整体CPU,GPU,RAM的使用情况。

我遇到了psutil包(https://pypi.org/project/psutil/),它可以分析正在使用的系统资源,但我无法设法让特定程序使用系统资源。我怎样才能做到这一点?

我也看过这个How to get current CPU and RAM usage in Python?,但它不是我想要的。

我的python代码如下:

def main():
    #Get current Process ID
    pid = os.getpid()
    #Get resources used by current process
    p = psutil.Process(pid)
    with p.oneshot():
        cpu_percent = p.cpu_percent()
        # Get Memory and GPU usage of this PID as well

1 个答案:

答案 0 :(得分:1)

根据其PID,您可以获取特定进程的CPU / RAM指标(而非GPU):

requirements = list(map(lambda x: x.split(';')[0], requirements)