[英]Piping iostat -d 1 to python sys.stdin
I am trying to pipe the output of utilities like iostat, mongostat, etc with the command: 我正在尝试使用以下命令通过管道传送iostat,mongostat等实用程序的输出:
$ iostat -d 1 | ./script.py
in which I am using the code: 我在其中使用代码:
for line in sys.stdin:
print line
I see that it hangs and does not print each line to the console. 我看到它挂起了,并且没有将每行打印到控制台。 if I run without the flag to repeat every second '-d 1' where output only happens once, the script behaves as expected.
如果我运行时不带标志重复执行仅输出一次的第二个“ -d 1”,则脚本的行为与预期的一样。
$ iostat | $ iostat | ./script.py
./script.py
The data is being buffered, you can call iter
on sys.stdout.readline
: 数据正在缓冲中,您可以在
sys.stdout.readline
上调用iter
:
import sys
for line in iter(sys.stdin.readline,""):
print line
Running iostat
on it's own just outputs a few lines, iostat -d 1
loops continuously so the data gets buffered. iostat
运行iostat
仅输出几行, iostat -d 1
连续循环,因此数据得到缓冲。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.