Is sed blocking?

徘徊边缘 提交于 2019-12-04 17:42:02

问题


I had the impression sed wasn't blocking, because when I do say:

iostat | sed

sed processes the data as it arrives, but when I do

iostat | sed | netcat

Then sed blocks netcat.

Am I right?


回答1:


sed will work in buffered mode when it doesn't print to a terminal. This means that it will try to fill its internal buffer before doing any processing and output by default.

This is done to increase throughput, because normally in a pipe you don't care about the timing, but want as much data processed in a given time as possible.

Passing -u to sed will tell it to work unbuffered, therefore working the same way it works when output goes to a terminal.




回答2:


In addition to what @saua says, sed is at least line oriented, that it, it read a line, then operates on it so it will always be buffering at least one line. In addition, sed can work in multiline mode. If you are using a multiline pattern, then sed can't output it's current buffer until it knows that the pattern either doesn't apply or the pattern has been processed.




回答3:


I don't know if I understand the question right, but in your example, it should be like this:

  • sed waits for iostat to send data
  • iostat will wait for sed to process the data if sed is slow
  • netcat waits for sed to send data
  • sed will wait for netcat again, if it is slow

Other than that, sed shouldn't need to read all its input to produce output.

Do you observe any delays that cannot be explained by this and some small buffering?




回答4:


stdbuf can assist with changing the buffering behavior of the standard io streams. You can try this to change the buffering behavior:

... | stdbuf -oL -eL sed -e ... | ...


来源:https://stackoverflow.com/questions/577865/is-sed-blocking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!