简体   繁体   中英

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. 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.

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. How can I achieve that?

I have also seen this How to get current CPU and RAM usage in Python? but its not what I want.

My python code as follows:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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