Skip to content Skip to sidebar Skip to footer

Python Subprocess.stdout.readline() Hang

I try simple code using subprocess on python, while calling process.stdout.readline() it will hang on it. How to solve this problem? While trying fcntl for nonblock in the output I

Solution 1:

Your desired output suggests that there is no newline after First number: and therefore the blocking proc.stdout.readline() call (before proc.stdin.write('5\n') line) hangs and non-blocking variant returns nothing (empty bytestring) -- there is no newline/EOF to return yet.

To fix it, you could stop reading at the end of the prompt (at the colon ':'). See subprocess readline hangs waiting for EOF

Even if there were a newline after the prompt; readline() may hang due to buffering issues. See Python C program subprocess hangs at "for line in iter"

For example, if test is equivalent to:

#!/usr/bin/env python3
a = int(input('First number: '))
b = int(input('Second number: '))
print('Answer:', a + b)

then to get the answer, you could use pexpect module:

#!/usr/bin/env pythonimport pexpect # $ pip install pexpectdefprint_result(locals):
    result = int(locals['child'].after.rpartition(b' ')[2])
    print(result)

pexpect.run('./test', events={'First number': '5\n', 'Second number': '5\n',
                              r'Answer:\s*\d+': print_result})

If you want to see both input and output; you could set logfile=sys.stdout parameter.

Solution 2:

I meet a similar issue; and fix it by adding flush() before readline.

               proc.stdout.flush()
               proc.stdout.readline()

Now, I can see the output right away; looks flush() put EOF there; then readline is ready to go.

Post a Comment for "Python Subprocess.stdout.readline() Hang"