Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用以下命令对 iostat、mongostat 等实用程序的输出进行管道传输:
$ iostat -d 1 | ./script.py
我在其中使用代码:
for line in sys.stdin: print line
我看到它挂起并且没有将每一行打印到控制台。如果我在没有标志的情况下运行以每秒重复一次'-d 1',其中输出只发生一次,则脚本的行为与预期相同。
$ iostat | ./script.py
The data is being buffered, you can call iter on sys.stdout.readline:
iter
sys.stdout.readline
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 -d 1