user961714
user961714

Reputation: 731

How to capture output from continuous process in Python?

I am new to Python and Linux. I have a process running in a terminal window that will go indefinitely. The only way to stop it would be for it to crash or for me to hit ctrl+C. This process outputs text to the terminal window that I wish to capture with Python, so I can do some additional processing of that text.

I know I need to do something with getting stdout, but no matter what I try, I can't seem to capture the stdout correctly. Here is what I have so far.

import subprocess

command = 'echo this is a test. Does it come out as a single line?'

def myrun(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    stdout = []
    while True:
        line = p.stdout.read()
        stdout.append(line)

        if line == '' and p.poll() != None:
                break
    return ''.join(stdout)

result = myrun(command)
print('> ' + result),

This will work when my command is a simple "echo blah blah blah". I am guessing this is because the echo process terminates. If I try running the continuous command, the output is never captured. Is this possible to do?

Upvotes: 0

Views: 1645

Answers (1)

georgexsh
georgexsh

Reputation: 16624

read() will block on reading until reach EOF, use read(1024) or readline() instead:

read(size=-1)

Read and return up to size bytes. If the argument is omitted, None, or negative, data is read and returned until EOF is reached.

eg:

p = subprocess.Popen('yes', stdout=subprocess.PIPE)

while True:
    line = p.stdout.readline()
    print(line.strip())

see more on the python io doc.

Upvotes: 2

Related Questions