From: Robert Klemme Date: 2009-11-07T02:50:05+09:00 Subject: Re: reading from an external process with IO.popen On 11/06/2009 04:01 PM, Robert Citek wrote: > On Fri, Nov 6, 2009 at 5:15 AM, Robert Klemme > wrote: >> 2009/11/5 Robert Citek : >>> However, if I change the cat command to a sed command, the ruby >>> version no longer works. The command-line equivalent does work, but >>> the ruby version waits forever and has to be interrupted: >> That's probably because you do not close the write end of the pipe in >> Ruby code. > > Perhaps, but what if I don't want to close the pipe? That is, I would > like to keep the pipe open so that I can send some data, read some > data and work on it, send some more data, read some more data and work > on it, etc. much like the process was a service, e.g. database. I am > trying to code the equivalent of a Call and Response. My examples > using cat and sed are just stand-ins for the real program. If the program you are using does not cooperate you're out of luck. For example, if it assigns a huge read buffer then you might have to send hundreds of lines before it even starts processing the first one. I have no idea how the implementation of sed that you are using does it but if you for example think of sort you _cannot_ get any output before the last line has been written and the write end of the pipe has been closed. > BTW, the cat example works as expected, but the using sed doesn't > work. That is, there is no output from sed until the pipe closes. > There seems to be some buffering going on. I'm guessing it's from the > Ruby side since I don't see this when run from the shell. But that's > just a guess. The shell closes the pipe as well. It is sed that is doing the buffering and you have no control over it unless it provides an option to control this. > Of course, it's entirely possible that IO.popen is not the "right" way > to tackle this and I have not discovered the Ruby way, yet. No, it's the right way but your expectations cannot be met in all cases. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/