Brian Candler
5/26/2007 1:45:00 PM
On Sat, May 26, 2007 at 06:23:55AM +0900, Stephan Wehner wrote:
> Vince H&K wrote:
>
> > This has nothing to do with pipes. Ruby is buffering output streams:
> > the executed command necessarily flushes its output when it terminates
> > but nothing in Ruby guarantees that the buffer in the (Ruby) stream will
> > be flushed before the start of the next command.
> >
> > I'll try to make it more clear: the buffering is done within Ruby, so
> > the underlying operating system knows nothing about the two lines you
> > want to output, and happily just writes what the next executed commands
> > writes.
> >
> > Hope this helps,
>
> You're saying the output from "some_executable" is passed directly on to
> the operating systems STDOUT, while what puts prints on stdout is
> buffered by Ruby?
If Ruby goes via the stdio library (fprintf and friends) then it will be
buffered by libc. If not (i.e. it uses write and friends) then it would make
sense for Ruby to add its own layer of buffering. In both cases this is so
that it's not horrendously inefficient when writing, say, lots of single
characters in a loop.
To defeat this, look at
IO#sync = true
Or call flush before you fork and exec any external programs which write to
the stdout they share with their parent.
Regards,
Brian.