2

I am trying to use IO.popen in order to put (with .puts method) and to get (with .gets method) messages from a process to its sub-process.

I am not very experimented and I have a question about. Having the following code, I have an error because it is not possible to write in a closed stream.

class Interface
  def initialize(path)
    @sub_process = IO.popen(path, 'w+')
  end

  def start!
    if ok?
      @sub_process.puts 'Hello', 'my name is ...'
      # and more...
    end
  end

  protected

  def ok?
    is_ready?(@sub_process) && is_cool?(@sub_process)
  end

  def is_ready?(sub_process)
    reply = process_command(sub_process, 'are u ready?')
    reply.chomp.match(/yes_i_am_ready$/)
  end

  def is_cool?(sub_process)
    reply = process_command(sub_process, 'are u cool?')
    reply.chomp.match(/yes_i_am_cool$/)
  end

  def process_command(sub_process, command)
    rdr = Thread.new { sub_process.read } # alternative: io.readlines
    sub_process.puts "#{command}"
    sub_process.close_write
    rdr.value # joins and fetches the result
  end
end

a = Interface.new("./program")
a.start!

(...) in `write': not opened for writing (IOError)

As we can see, this error occur during is_cool? test (as explained at: http://ruby-doc.org/core/classes/IO.html#M002289).

But if I try to comment in process_command method the line:

# sub_process.close_write

the script seems to sleep... infinitely :s

I believe that it is not possible to open again a closed stream. And I can't create an other IO.popen instance of my program "./program" because it needs to be initialized with some command (like 'are u ready?' and 'are u cool?') at the beginning, before I use it (by sending and receiving messages like a simple discussion).

How changes can I do over the current code in order to solve this problem?

Edit: in other words, I would like to establish a such communication (according to a given protocol):

Parent message:                Child answer:
--------------                 ------------

'are u ready?'                 'yes_i_am_ready'
'are u cool?'                  'yes_i_am_cool'
'Hello'                        'foo'
'my name is ...'               'bar'

Many thanks for any help.

4

3 回答 3

2

也许有一个可行的例子会有所帮助。这是一个,经过测试并且已知可以在 Linux 上的 MRI 1.8.7 中工作。

bar.rb

#!/usr/bin/ruby1.8

begin
  loop do
    puts "You said: #{gets}"
    $stdout.flush
  end
rescue Errno::EPIPE
end

foo.rb

#!/usr/bin/ruby1.8

class Parent

  def initialize
    @pipe = IO.popen(CHILD_COMMAND, 'w+')
  end

  def talk(message)
    @pipe.puts(message)
    response = @pipe.gets
    if response.nil?
      $stderr.puts "Failed: #{CHILD_COMMAND}"
      exit(1)
    end
    response.chomp
  end

  private

  CHILD_COMMAND = './bar.rb'

end

parent = Parent.new
puts parent.talk('blah blah blah')
puts parent.talk('foo bar baz')

foo.rb 输出

You said: blah blah blah
You said: foo bar baz
于 2010-03-02T16:37:18.917 回答
0

使用这种形式有帮助Thread.new吗?

rdr = Thread.new(sub_process) {|x| x.readlines }
于 2010-03-02T17:14:05.947 回答
0

一个封闭的IO不能再使用了。IO如果您打算继续使用它,则不应关闭它。

如果您删除IO#close_write了以下行中的代码仍然存在问题。

rdr = Thread.new { sub_process.read }

IO#read读到EOF。因此,在流关闭之前,它永远不会终止。您在代码中提到IO#readline,这将是更好的选择。仅当 popend 进程从不发送换行符时,使用IO#readline您的程序才会挂起。

popen 的另一个问题如下。IO#popen创建一个新的过程。进程可能会被您、其他用户、内存不足等杀死。不要期望您的流程始终运行。如果进程被杀死IO#readline会抛出一个EOFError,IO#read会返回 imidiatley。您可以使用以下代码确定终止原因。

Process::wait(io.pid)
status= $?
status.class # => Process::Status
status.signaled? # killed by signal?
status.stopsig # the signal which killed it
status.exited # terminated normal
status.exitstatus # the return value
status.ki
于 2010-03-02T12:31:25.683 回答