I use Python subprocess
to fork a C application and open a pipe to stdout
.
app = subprocess.Popen(args, stdout=subprocess.PIPE)
The application writes several lines to stdout
like so:
printf("Line 0\n");
printf("Line 1\n");
printf("Line 2\n");
I try to read these lines in my Python script before the application exits:
line = app.stdout.readline()
However, readline
blocks indefinitely without returning any content, even though I expect to read Line 0
, Line 1
, and Line 2
, in three separate calls to readline
. I notice that when the application finally exits, readline
returns the expected contents. However, I want readline
to return the expected contents as soon as they are passed to printf
. What is happening?