How to monitor subprocess's stdout in real time

Question:

I have a subprocess that generate images. A main programe will consume images.

My plan is to launch subprocess, monitor it. Once there are several images available (i.e. subprocess printed ‘2’ or ‘3’), I will start main programe.

However, I fail to get ‘real-time’ output from subprocess. Every time, subprocess will not return anything via PIPE until it has generated all 20 images. Except in debug-mode.

in sub_process.py:

import time
for i in range(20):
    time.sleep(1)
    print(i)
    # generate one image each second

main process: ready to go on processing images if there is at lease one image.

from subprocess import PIPE
from subprocess import Popen

p= Popen(['python', "./subprocess.py"], stdout=PIPE, stderr=PIPE) 

while True:
    if '2' in p.stdout.readline().decode("utf-8"):
    print('enough image')
    break
print('go on processing images generated by subprocess.py')
Asked By: zheyuanWang

||

Answers:

You need to add flush=True in subprocess.py as parameter to print:

import time
for i in range(20):
    time.sleep(1)
    print(i, flush=True)
    # generate one image each second

Note: if you name your file subprocess.py, then on your other program your subprocess import will fail, because it tries to import from your subprocess.py, not from the actual subprocess library.

I recomand puting a p.kill() on the end to stop your image generator

Answered By: Lima
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.