f



[P]Open4 and stdin / stdout

I want to have a first script 'popen4-test.rb' calling another one 
'inout-test.rb' with an input dialog.

'inout-test.rb' ask a question "What is the recipe type?"
and wait for the answer, here is 'inout-test.rb' :
--------------------------------------------------------------------------------
#!/usr/bin/env ruby
# encoding: utf-8

require 'rubygems'

$stdout.puts "What is the recipe type?"
sleep 1.0
r_type = $stdin.gets.chomp
$stdout.puts "Recipe type: '#{r_type}'."
sleep 5.0
$stdout.puts "Bye!"
--------------------------------------------------------------------------------
Notice that 'inout-test.rb' works as expected when called directly by 
"./inout-test.rb".

here is 'popen4-test.rb' :
--------------------------------------------------------------------------------
#!/usr/bin/env ruby
# encoding: utf-8

require 'rubygems'
require "open4" # or popen4

cmd = '/home/yt/bin/tests/inout-test.rb'
status =
       Open4::popen4(cmd) do |pid, stdin, stdout, stderr|
         out = stdout.read.strip
         if out.include? 'type'
           puts out
           r_type = gets.chomp
           stdin.puts r_type
         end
         stdin.close if out.include? 'Bye!'
         puts "pid        : #{ pid }"
         puts "stderr     : #{ stderr.read.strip }"
       end

     puts "status     : #{ status.inspect }"
     puts "exitstatus : #{ status.exitstatus }"
--------------------------------------------------------------------------------

Then, 'popen4-test.rb' is supposed to forward the question to the terminal.
But this does not work, 'popen4-test.rb' hangs if 'inout-test.rb' has a 
"$stdin.gets".
However if I comment out the lines, in 'inout-test.rb', related to this 
"$stdin.gets", 'popen4-test.rb' does not hang any more.

Something wrong in my test, but what?

The purpose of this test is to simulate a socket server asking for a 
script to be launched (here 'inout-test.rb') and forwardind the 
questions to the client...
0
UTF
6/23/2016 6:59:50 AM
comp.lang.ruby 48885 articles. 0 followers. Post Follow

0 Replies
618 Views

Similar Articles

[PageSpeed] 48

Reply: