On both my Linux and OSX system, it seems that there's a large delay reading from stdin if the process being read from is still running, even if it's producing output to be read. It will wait until it has a really large amount of data, and then suddenly it will read it all, process it all, and then idle again waiting for a huge chunk of data. I think that it's something that either Python or I am doing because when I pipe the output through awk it processes it in real-time.
Anyhow, here's the source for my *very simple* test to try to get the pipe working:
Expand|Select|Wrap|Line Numbers
- while 1:
- print "input: " + raw_input()
sudo tcpdump -i en1 -xx -l |awk '{print $2$3$4$5$6$7$8$9}' | python test.py
That command will hang until there's a good amount of data. Removing the python script from the end produces real-time awk formatted data without delay.
I've also tried this for the script so that I'm only reading a single character at a time, same problem:
Expand|Select|Wrap|Line Numbers
- import sys
- buffer = []
- while 1:
- char = sys.stdin.read(1)
- if not char == "\n":
- buffer.append(char)
- else:
- print "input: " + "".join(buffer)
- buffer = []