Currently I'm writing to stdin in a loop when stdout provides me with the last line marker "EE\n". Now it should go on until the loop is terminated but for some reason stdout is not providing any new lines at iteration 625 and so on, I think the problem is with stdin not listening or capturing the new puts.
I've tried to do it with PTY.spawn instead of Open3.popen3 but no luck there. Also I've set a timeout in expect and when this occurs to write again to stdin. I got the idea to keep stdin open from this post: ruby popen3 -- how to repeatedly write to stdin & read stdout without re-opening process?
I need to keep stdin open because the next step will be that stdout is providing the new information for stdin and so on.
def testen()
Open3.popen3("./pos.tx") do |stdin, stdout, stderr, wait_thr|
stdin.sync = true
stdout.sync = true
stdin.puts "e0"
count = 0
while true
stdout.expect("\n") do |result|
if result[0] == "EE\n"
# sleep(0.1)
count +=1
print count.to_s+"\n"
stdin.puts "e0"
end
end
end
stdin.close
# status = wait_thr.value
stdout_str = stdout.read
print stdout_str
end
end
testen
So this piece of code results in a final print of 625 which means there where 625 successful stdin.puts "e0"
. But it should go on and on, until some user interference like ctrl c
.
Solution
I was not reading from stderr which resulted a full pipe, now I use popen2.