How to get output from subprocess.Popen(). proc.stdout.readline() blocks, no data prints out

后端 未结 4 1751
自闭症患者
自闭症患者 2020-11-28 05:32

I want output from execute Test_Pipe.py, I tried following code on Linux but it did not work.

Test_Pipe.py

import time
while True :         


        
4条回答
  •  粉色の甜心
    2020-11-28 06:27

    Test_Pipe.py buffers its stdout by default so proc in Caller.py doesn't see any output until the child's buffer is full (if the buffer size is 8KB then it takes around a minute to fill Test_Pipe.py's stdout buffer).

    To make the output unbuffered (line-buffered for text streams) you could pass -u flag to the child Python script. It allows to read subprocess' output line by line in "real-time":

    import sys
    from subprocess import Popen, PIPE
    
    proc = Popen([sys.executable, "-u", "Test_Pipe.py"], stdout=PIPE, bufsize=1)
    for line in iter(proc.stdout.readline, b''):
        print line,
    proc.communicate()
    

    See links in Python: read streaming input from subprocess.communicate() on how to solve the block-buffering issue for non-Python child processes.

提交回复
热议问题