anjali ahlawat
anjali ahlawat

Reputation: 95

How to read data from the stdout after each input when using Open3.popen3?

Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thread|
    stdout.sync = true;
 
Thread.new do
   stdout.each.with_index {|line, line_no| updateParameters(line) if line_no == $lineStartIndex}
 end

 stdin.puts "e #{$initialState}"

 stdin.puts "e #{$nextState}"
 
 stdin.puts "e #{$nextState}"

 stdin.close

 wait_thread.value
end

In the following code, I want to read the data from stdout after each stdin. My next input depends on what I get in the last output. I want to call updateParameters() function to update the nextstate which will be used for the next input. But currently I get the output only after the input process is complete. Hence nextState is not getting updated and I'm getting wrong output.

I understand that currently all the data is stored by ruby in bufffer and the entire output is returned after receiving all the inputs.

But is there a way to read the output here after each input command?

Upvotes: 0

Views: 234

Answers (0)

Related Questions