On Thu, Nov 5, 2009 at 4:47 PM, Craig Buchek <craig.buc
...@gmail.com> wrote:
> It took me a long time to find the answer, but you have to close the
> write end of the pipe before you can read from it.
That's unfortunate. 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.
> ...but calling IO#sync=true on the object didn't help.
Tried that, also, and noticed it didn't help.
> Anyway, the following works. I'm not sure there's any simple way to
> process the stream in real-time though. I also converted it to more
> idiomatic Ruby, which is much shorter.
And that does work, sort of. The output "hello" and "world" do
appear, but only after the 2 second pause. I also tried 'cat -n' at
the shell and it also pauses before the output:
$ { echo hello ; sleep 2 ; echo world; } | cat -n
Makes me wonder if buffering the output is a feature of the shell or
program or system and if that feature can be temporarily overridden.
It would be nice to do something like this:
open_pipe
set_of_data.each do |foo|
send_foo_to_pipe
flush_pipe
read_foo_output_from_pipe
process_foo_output
end
close_pipe
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.
Again, any pointers in the right direction are greatly appreciated.
Regards,
- Robert