1

I am writing a benchmarking utility using python. I would like my python program to execute a program and capture the output as well as get its max resource usage. The following code runs a command and gets its memory and processor usage once per second, but how do I also access the output inside python? Here is my current setup:

pid = psutil.Popen(cmd_args, shell=False, stdout=subprocess.PIPE)

p = psutil.Process(pid=pid.pid)
x = 0
cpu_util = []
mem_util = []

while p.status() != "zombie":
    if x % 10 == 0:
        with p.oneshot():
            cpu_util.append(p.cpu_percent())
            mem_util.append(p.memory_percent())
    x += 1
    time.sleep(0.1)

max_cpu_load = max(cpu_util)
max_mem_load = max(mem_util)

#What I would like to do, but don't understand
print(pid.stdout)
mcragun
  • 1,198
  • 3
  • 10
  • 17
  • Do you want to export the output to a file? What do you mean by "get the output"? – Yu Zhang Dec 08 '17 at 01:22
  • This is similar to the question at https://stackoverflow.com/questions/38630421/how-to-execute-shell-command-and-stream-output-with-python-and-flask-upon-http-r so you may find the answer there helpful. – Matthias Fripp Dec 08 '17 at 01:27
  • @YuZhang, thanks, I updated my question. I would like to access the stdout and stderr inside my python program. – mcragun Dec 08 '17 at 16:48
  • Use `Popen.communicate()`. This related to: https://stackoverflow.com/questions/31833897/python-read-from-subprocess-stdout-and-stderr-separately-while-preserving-order – cowbert Dec 08 '17 at 17:52

1 Answers1

0

Assuming you have a Unix box, and you can get the file descriptors of relevant stdin, stdout, ..., you may use the select.select function to monitor the activity on these streams.

https://docs.python.org/3/library/select.html#select.select

glenfant
  • 1,298
  • 8
  • 9