Monitor child process resources over time in python… here is a solution to the problem.
Monitor child process resources over time in python
I’m currently trying to monitor the memory usage of my child processes over time to generate graphs.
This code seems to fix the problem, but I don’t really like the “over time” loop I made because you can see it’s a basic while true
loop.
from subprocess import Popen
import psutil
process = Popen(["python", "dummy_script.py"])
ps = psutil. Process(process.pid)
while process.poll() is None:
print(ps.memory_percent())
I want to tweak this code to be able to check the memory usage of my child processes at regular intervals (e.g. 1 second).
This is the first time I’ve used both subprocess
and psutil
, so it’s hard for me to understand how to use it properly.
Note: The dummy_script.py here is not interesting
import numpy
import time
result = []
for i in range(10240):
result.append(numpy.random.bytes(1024*1024))
print(len(result))
Solution
Let the program sleep in a while loop for 1 second
import time
while process.poll() is None:
print(ps.memory_percent())
time.sleep(1)