Python subprocess hangs with "for line in iter"
In this scenario, the Python script is intended to execute a C program and receive its output. However, the script hangs at the line "for line in iter(process.stdout.readline, '')".
The Buffering Issue
The problem arises from a buffering issue. In C programs, stdout is typically block buffered when output is piped. This means that the output is buffered until a certain amount is written or a newline is encountered.
In the C program, each iteration of the while loop prints "2000" and sleeps for a second. Since no newline is printed, the output is buffered.
For Line in Iter Issue
In Python, subprocess.stdout.readline() reads the stdout of the child process and returns a line. The "for line in iter()" loop iterates over the lines returned by readline().
However, since the C program's output is not being flushed due to buffering, there are no lines to read at the point where the Python script enters the loop. Hence, the script waits indefinitely at this line.
Solutions
Fix Buffering in C Program
setvbuf(stdout, (char *) NULL, _IOLBF, 0);
stdbuf -oL ./main
Pseudo-TTY
import pty master_fd, slave_fd = pty.openpty() process = Popen("./main", stdin=slave_fd, stdout=slave_fd, stderr=STDOUT, bufsize=0, close_fds=True)
import pexpect child = pexpect.spawn("./main") for line in child: print line
By applying one of these solutions, the buffering issue will be resolved, allowing the Python script to read the output of the C program as expected.
The above is the detailed content of Why Does My Python `subprocess` Hang When Reading Output from a C Program Using `for line in iter(process.stdout.readline, \'\')`?. For more information, please follow other related articles on the PHP Chinese website!