ruby popen3 — how to repeatedly write to stdin & read stdout without re-opening process?

前端 未结 1 1784
无人及你
无人及你 2021-01-17 16:39

I am using Open3\'s popen3 method to start a process that functions in a console-like / REPL fashion to repeatedly accept input and return output.

I am able to open

相关标签:
1条回答
  • 2021-01-17 17:09

    You can have some success using expect library, and have the child process to explicitly mark the end of each output, like:

    require 'expect'
    require 'open3'
    
    Open3.popen3("/bin/bash") do
        | input, output, error, wait_thr |
        input.sync = true
        output.sync = true
    
        input.puts "ls /tmp"
        input.puts "echo '----'"
        puts output.expect("----", 5)
    
        input.puts "cal apr 2014"
        input.puts "echo '----'"
        puts output.expect("----", 5)
    end
    

    As a bonus, expect has a timeout option.

    0 讨论(0)
提交回复
热议问题