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 the process, send input, and receive the output just fine, with code like this:
Open3.popen3("console_REPL_process") do |stdin, stdout, stderr, wait_thr|
stdin.puts "a string of input"
stdin.close_write
stdout.each_line { |line| puts line } #successfully prints all the output
end
I want to do that many times in a row, without re-opening the process, as it takes a long time to start up.
I know I have to close stdin in order for stdout to return.. but what I don't know is, how do I 'reopen' stdin so I can write more input?
Ideally I want to do something like this:
Open3.popen3("console_REPL_process") do |stdin, stdout, stderr, wait_thr|
stdin.puts "a string of input"
stdin.close_write
stdout.each_line { |line| puts line }
stdin.reopen_somehow()
stdin.puts "another string of input"
stdin.close_write
stdout.each_line { |line| puts line }
# etc..
end
Thanks to pmoo's answer, I was able to devise a solution using PTY
and expect
, expecting the prompt string that the process returns whenever it is ready for more input, like so:
PTY.spawn("console_REPL_process") do |output, input|
output.expect("prompt >") do |result|
input.puts "string of input"
end
output.expect("prompt >") do |result|
puts result
input.puts "another string of input"
end
output.expect("prompt >") do |result|
puts result
input.puts "a third string of input"
end
# and so forth
end
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With