简体   繁体   中英

Python Popen stdin PIPE gets spamed

I have two simple programs:

test.sh

rm ~/out.txt
for ((i=0; i<10; i++)); do
  read j
  echo "read: '$j'" >> ~/out.txt
done

And test.py

import sub
process
proc = subprocess.Popen('/Users/plg/test.sh', stdin=subprocess.PIPE)
proc.stdin.write('1\n')
proc.stdin.write('2\n')

When I run test.py (using Python 2.7.2), ~/out.txt contains this:

read: '1'
read: '2'
read: ''
read: ''
read: ''
...

Why does test.sh receive the last 8 lines? It should get stuck and wait for input. But apparently, Popen spams '\\n' once I wrote something and Python exits.

I can't find a fix for this, using proc.stdin.flush() and proc.stdin.close() doesn't do any good. How do I prevent this?

Popen is not spamming any output, when your Python program exits test.sh will receive an EOF (end-of-file) indicating that there is nothing left to read, at which point the read command in test.sh will give an empty string on each call and give an exit status code of 1.

It doesn't really make sense to have test.sh block on input that will never happen, you would be better off checkin the status code of read and exiting if you encounter an EOF or other read error:

rm ~/out.txt
for ((i=0; i<10; i++)); do
  read j
  if [ $? != 0 ]; then
    break
  fi
  echo "read: '$j'" >> ~/out.txt
done
import subprocess
proc = subprocess.Popen('/Users/plg/test.sh', stdin=subprocess.PIPE)
proc.stdin.write('1\n')
proc.stdin.write('2\n')
proc.wait()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM