Cross-platform resource usage on subprocess.Popen

扶醉桌前 提交于 2019-12-11 11:02:31

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/22715059/cross-platform-resource-usage-on-subprocess-popen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!