STDOUT/STDERR mixed up in console

When I'm running ruby code through IDEA, the STDOUT/STDERR input is often out-of-order. If I follow the example of the unit tests, I set my Ruby arguments to:

-e "STDOUT.sync=true;STDERR.sync=true;load('myfile.rb')"

Yet the output is always in the wrong order. If I run the same command from the Windows command prompt, the output is always right. Here's an example ruby script to try:

$stderr.puts "1:err"
$stderr.puts "2:err"
$stdout.puts "3:out"
$stderr.puts "4:err"
$stdout.puts "5:out"
$stdout.puts "6:out"
$stdout.puts "7:out"
$stdout.puts "8:out"
$stdout.puts "9:out"
$stderr.puts "10:err"
$stdout.puts "11:out"

Is there a setting that I am missing?

Thanks!

Brian

1 comment
Comment actions Permalink

Hello Brian,

I can reproduce the same thing. This problem is concerned with buffering of process's input / output streams. Console flushes buffer faster, than JVM (JVM should send and dispatch events, etc.).

You can try to run such script (with -e "STDOUT.sync=true;STDERR.sync=true;load('myfile.rb')" option)
$stderr.puts "1:err"
sleep(0.1)
$stderr.puts "2:err"
sleep(0.1)
$stdout.puts "3:out"
sleep(0.1)
$stderr.puts "4:err"
sleep(0.1)
$stdout.puts "5:out"
sleep(0.1)
$stdout.puts "6:out"
sleep(0.1)
$stdout.puts "7:out"
sleep(0.1)
$stdout.puts "8:out"
sleep(0.1)
$stdout.puts "9:out"
sleep(0.1)
$stderr.puts "10:err"
sleep(0.1)
$stdout.puts "11:out"

in this case output will be in right order.

0

Please sign in to leave a comment.