aaronqli
aaronqli

Reputation: 809

Scala process: redirected stdout and stderr are always delayed

I am writing a simple application that executes an external command (that takes very long time to run) while redirecting its output to another place in real time. However, no matter what I do (see below), the output from stdout and stderr always seem delayed (by 20 seconds to 1 minute). I suspect Scala only redirects the output when it is accumulated to a certain amount, but I do not know how it works under the hood. Is there any way to fix this?

I have tried

var lines = Seq("somecommand",blah) lines_!
dosomethingelse(lines.map{e=>println(e);e}) //problem: does not print line by line! It prints a lot of lines each time

And

var p = Seq("somecommand",blah) run ProcessLogger((o:String)=>{println(o)},(e:String)=>{println(e)}) // Same problem

And

var p = Seq("somecommand",blah) run ProcessIO(...) //manipulate java.io.InputStream directly, print each byte once it is immediately available. Same problem as above.

Upvotes: 1

Views: 1766

Answers (2)

aaronqli
aaronqli

Reputation: 809

Turns out this is because of a underlying stdout issue with the command executed. Scala functions all work correctly.

Upvotes: 1

WestCoastProjects
WestCoastProjects

Reputation: 63082

How about flush()'ing the stdout after each line (or some configurable number of lines

Console.flush(); 

So:

var lines = Seq("somecommand",blah) lines_!
dosomethingelse(lines.map{e=>println(e);Console.flush(); e})

Upvotes: 2

Related Questions