TheCoder
TheCoder

Reputation: 87

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

Upvotes: 3

Views: 9072

Answers (1)

Giampaolo Rodolà
Giampaolo Rodolà

Reputation: 13056

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()

Upvotes: 1

Related Questions