简体   繁体   English

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

[英]How to get current CPU, GPU and RAM usage of a particular program in Python?

I have written a python program to detect faces of a video input (webcam) using Haar Cascade. 我编写了一个python程序来检测使用Haar Cascade的视频输入(网络摄像头)的面部。 I would like to know how much CPU, GPU and RAM are being utilized by this specific program and not the overall CPU, GPU, RAM usage. 我想知道这个特定程序使用了多少CPU,GPU和RAM,而不是整体CPU,GPU,RAM的使用情况。

I came across psutil package ( https://pypi.org/project/psutil/ ) which enables to profile system resources being used but I could not manage to get system resources being used by a specific program. 我遇到了psutil包( https://pypi.org/project/psutil/ ),它可以分析正在使用的系统资源,但我无法设法让特定程序使用系统资源。 How can I achieve that? 我怎样才能做到这一点?

I have also seen this How to get current CPU and RAM usage in Python? 我也看过这个如何在Python中获取当前的CPU和RAM使用率? but its not what I want. 但它不是我想要的。

My python code as follows: 我的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

You can get CPU/RAM metrics (not GPU) for a particular process given its PID: 在给定PID的情况下,您可以获得特定进程的CPU / RAM指标(不是GPU):

import psutil
psutil.Process(1234).cpu_percent()
psutil.Process(1234).memory_info()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM