Home > Software engineering >  Execute cmd from python and get continuous output
Execute cmd from python and get continuous output

Time:03-22

here is my problem: I need to execute a cmd command from python, the command is not something that executes fast, it will be running for a longer period of time. What I need is a way to #1 execute the command and #2 get the continuous output of that command.

Example of the output:
some text... (then about half a minute wait)
some other text... (waiting for some time)
more text...

You get the idea, the output comes about every 30 seconds. I need to catch all of the outputs and do something with them. Is there a way to do this?

I know I can execute the command with os.system('command') but i'm struggling to find a way to read the output!

CodePudding user response:

A simple solution is given in this answer. In python 3 it would be something like:

#!/usr/bin/env python3
import subprocess

proc = subprocess.Popen('./your_process', stdout=subprocess.PIPE)

while True:
    line = proc.stdout.readline()
    if not line:
        break
    else: 
        # Do whathever you need to do with the last line written on 
        # stdout by your_process
        print("doing some stuff with...", line)
        print("done for this line!")

of course, you'd still need to take into account how the producer process buffers stdout, i.e. if the subprocess is something like

#include <stdio.h>
#include <unistd.h>

int main()
{
    for (int i = 1; i < 10000000; i  ) {
        printf("ping");
        sleep(1);
    }
}

the consumer script will just read a lot of "ping"s all together (which can be resolved with a fflush(stdout) in the loop.)

A much more complicated solution can be found in this answer.

  • Related