Потратив немного времени и усилий на поиск, я обнаружил, что можно получить анализ использования ресурсов подпроцесса.
Обновленный процесс P2 выглядит следующим образом:
# run.py
# Execute sub.py 10 times
# Import the required utilities
import psutil as ps
import time
from subprocess import PIPE
# define the command for the subprocess
cmd = ["python3", "sub.py"]
for i in range(10):
# Create the process
process = ps.Popen(cmd, stdout=PIPE)
peak_mem = 0
peak_cpu = 0
# while the process is running calculate resource utilization.
while(process.is_running()):
# set the sleep time to monitor at an interval of every second.
time.sleep(1)
# capture the memory and cpu utilization at an instance
mem = process.memory_info().rss/ (float)(2**30)
cpu = process.cpu_percent()
# track the peak utilization of the process
if mem > peak_mem:
peak_mem = mem
if cpu > peak_cpu:
peak_cpu = cpu
if mem == 0.0:
break
# Print the results to the monitor for each subprocess run.
print("Peak memory usage for the iteration {} is {} GB".format(i, peak_mem))
print("Peak CPU utilization for the iteration {} is {} %".format(i, peak_cpu))