I'm writing a python script that reads a stream from stdin and passes this stream to subprocess
for further processing. The problem is that python hangs after having processed the input stream.
For example, this toy program sorter.py
should read from stdin and pass the stream to subprocess
for sorting via Unix sort
:
cat dat.txt | ./sorter.py
Here's sorter.py:
#!/usr/bin/env python
import subprocess
import sys
p= subprocess.Popen('sort -', stdin= subprocess.PIPE, shell= True)
for line in sys.stdin:
p.stdin.write(line)
sys.exit()
The stream from cat
is correctly sorted but the programs hangs, i.e. sys.exit() is never reached.
I've read quite a few variations on this theme but I can't get it right. Any idea what is missing?
Thank you!
Dario