Skip to content Skip to sidebar Skip to footer

How Do I Monitor Stdout With Subprocess In Python?

I have a linux application that runs interactively from commandline using stdin to accept commands. I've written a wrapper using subprocess to access stdin while the application is

Solution 1:

Read from p.stdout to access the output of the process.

Depending on what the process does, you may have to be careful to ensure that you do not block on p.stdout while p is in turn blocking on its stdin. If you know for certain that it will output a line every time you write to it, you can simply alternate in a loop like this:

while still_going:
    p.stdin.write('blah\n')
    print p.stdout.readline()

However, if the output is more sporadic, you might want to look into the select module to alternate between reading and writing in a more flexible fashion.

Post a Comment for "How Do I Monitor Stdout With Subprocess In Python?"