flushing buffer

1 view
Skip to first unread message

Robert Citek

unread,
Feb 4, 2010, 5:19:43 PM2/4/10
to stl...@googlegroups.com
I've been playing with background jobs and came across this
interesting buffering phenomenon in bash:

$ { echo "1-"
for i in {1..100} ; do
yes -- -abcdefghijklmnopqrstuvwxyz |
head -10000 &
done
} | grep -E ^.+- | head
1-
-abcdefghijk-abcdefghijklmnopqrstuvwxyz
-abcdefghijklmno-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz
-abcdefg-abcdefghijklmnopqrstuvwxyz

That makes sense as the output buffers could flush at any time and do
not have to end on line endings. That's why there are minus signs in
between the characters.

To force "line buffering" apparently I can pipe the output through
perl with the "flush buffers" option turned on:

$ { echo "1-"
for i in {1..100} ; do
yes -- -abcdefghijklmnopqrstuvwxyz |
head -10000 |
perl -pe '$|=1'
done
} | grep -E ^.+- | head
1-

My ruby question is, can I replace the perl with ruby and, if so, how?

Regards,
- Robert

Robert Citek

unread,
Feb 4, 2010, 5:28:01 PM2/4/10
to stl...@googlegroups.com
On Thu, Feb 4, 2010 at 5:19 PM, Robert Citek <robert...@gmail.com> wrote:
> $ { echo "1-"
>  for i in {1..100} ; do
>    yes -- -abcdefghijklmnopqrstuvwxyz |
>    head -10000 |
>    perl -pe '$|=1'
>  done
> } | grep -E ^.+- | head
> 1-
>
> My ruby question is, can I replace the perl with ruby and, if so, how?

Arggggh. Such is my luck today: google for ten minutes, find nothing.
Send post to list, then go back to google, and find exactly what I'm
looking for: IO#sync

http://ruby-doc.org/core/classes/IO.html#M002263

$ { echo "1-"
for i in {1..100} ; do
yes -- -abcdefghijklmnopqrstuvwxyz |
head -10000 |

ruby -pe 'STDOUT.sync = true' &


done
} | grep -E ^.+- | head
1-

Does ruby have a shorthand version to specify autoflush?

Regards,
- Robert

Reply all
Reply to author
Forward
0 new messages