Skip to content
Advertisement

python subprocess.stdout.readline doesn’t

I have a piece of python code that calls a binary via the subprocess module. This binary reads data from stdin and outputs other data to stdout, which should then be collected by the python code.

Here’s how I use this binary in a shell:

JavaScript

with a return after each line (including the last), from which I get the output

JavaScript

which is again terminated by a newline. However, trying to put this in python code like this:

JavaScript

I receive exactly this:

JavaScript

The program is hanging, obviously waiting for some sort of communication to happen. This is partially intended – the check binary is supposed to stay active and wait for further input, returning the output of the calculation as soon as a further complete input is supplied – but since the first input is complete, I should have received the result of the calculation, which I obviously didn’t, or else we would have seen the printout. When I break the execution manually with CTRL+C, it tells me

JavaScript

What’s going on here? Is this a problem with the way I handle the pipes? Is this a problem with a missing EOF or missing newline?

PS: I found this issue Python subprocess.stdout.readline() hang, which is possibly the same, but didn’t receive an answer.

PPS: In case it matters, the python version is Python 2.6.6.

Advertisement

Answer

Have you tried using p.stdout.read() instead of readline()? It works for the following test code where invoker.py creates a pipe with invoked.py.

invoked.py

JavaScript

invoker.py

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement