Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming pipes in Python

Tags:

python

I'm trying to convert the output of vmstat into 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'm having is it blocks while trying to iterate sys.stdin. It seems like the input buffer doesn't get flushed. I don't want to endlessly loop around and burn processor time as I'm trying to measure this. Here's a simple demonstration which blocks on line 3:

import sys

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

Is there an easy way to access the stream immediately like grep does, without pausing while the input buffer fills up?

like image 809
Gareth Davidson Avatar asked Nov 15 '10 18:11

Gareth Davidson


People also ask

What is streaming in Python?

Streams are high-level async/await-ready primitives to work with network connections. Streams allow sending and receiving data without using callbacks or low-level protocols and transports.

What are pipes in Python?

Pipe is a Python library that enables you to use pipes in Python. A pipe ( | ) passes the results of one method to another method. I like Pipe because it makes my code look cleaner when applying multiple methods to a Python iterable. Since Pipe only provides a few methods, it is also very easy to learn Pipe.

Does Python have a pipeline?

The pipeline is a Python scikit-learn utility for orchestrating machine learning operations. Pipelines function by allowing a linear series of data transforms to be linked together, resulting in a measurable modeling process.


1 Answers

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

Use this instead:

for line in iter(sys.stdin.readline, ""):
    print line
like image 54
fabrizioM Avatar answered Oct 21 '22 04:10

fabrizioM