Question

First of all if this has been asked before I'm sorry for the duplicate, but I couldn't find the answer to my question anywhere.

So, I am pretty new to Python, and I am currently working on a wrapper for a web application. This wrapper starts a subprocess using Popen and then sends information about that subprocess to the web application. Everything is working great so far.

Now the only question I have is; Is it possible to get resource usage of a subprocess (RAM and CPU)?
If so, how would I go about doing that? Something cross-platform would be awesome, but a different method for Windows, Mac and Linux would work as well.

If it isn't possible to get via subprocess. How would I got about fetching resource usage from another process?

Any help would be appreciated!
Thanks for your time!

Was it helpful?

Solution

psutil provides a cross-platform solution to get resource usage of a subprocess e.g.:

import random
import psutil # $ pip install psutil

p = psutil.Process(random.choice(psutil.pids()))
print(p.name())
print(p.cpu_times())
print(p.memory_info())

to get the info for an existing subprocess.Popen instance just pass popen_instance.pid to psutil.Process. You could also create a subprocess using psutil.Popen directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top