Python Streaming Tubes

I am trying to convert vmstat output to a CSV file using Python, so I use something like this to convert to CSV and add the date and time as coloumns:

vmstat 5 | python myscript.py >> vmstat.log 

The problem I encountered is blocking while trying to iterate sys.stdin. It seems that the input buffer is not cleared. I do not want to loop endlessly and record the processor time, as I try to measure this. Here's a simple demo that blocks line 3:

 import sys for line in sys.stdin: sys.stdout.write(line) sys.stdout.flush() 

Is there an easy way to directly access the stream, as grep does, without pausing when the input buffer is full?

+7
python
source share
1 answer

VMstat 5 does not close stdout, so the python buffer is still waiting for more data.

Use this instead:

 for line in iter(sys.stdin.readline, ""): print line 
+7
source share

All Articles