1

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.

Sebas
  • 11
  • 3
  • It looks like you can set `$expect_verbose=true` to see all stdout output. That could help you verify if the subprocess itself is hanging. – Max Jan 10 '19 at 14:21
  • @Max thanks for the comment, it indeed seems like the subprocess itself is hanging. Do you maybe know how I could fix that? I don't really want to close anything, like stdin. – Sebas Jan 10 '19 at 14:41
  • I assume you've verified that the process doesn't normally hang after 625 inputs? My only idea is to avoid using popen3 if that is the issue. Maybe have the subprocess output to a named pipe that you read separately? – Max Jan 10 '19 at 15:03
  • 1
    @Max Thanks! That was kind of the issue I was not reading from stderr which would result in a pipe that filled up. Now I use popen2. – Sebas Jan 10 '19 at 15:19

0 Answers0