From: Eikichi On Date: 2010-03-02T08:33:23+09:00 Subject: Re: IO.popen with Threads Thank you very much for your answer, Robert. Thanks to your help, I could start coding. But I have another little problem and I would appreciate your opinion. Here is the code I used: class Test def initialize(program) while isready?(program) && cool?(program) program.puts 'start', 'my name is ...' # ... end end protected def process_command(program, command) begin rdr = Thread.new { program.read } # alternative: io.readlines program.puts "#{command}" program.close_write rdr.value # joins and fetches the result end end def isready?(program) reply = process_command(program, 'isready') reply.chomp.match(/yes_i_am_ready$/) end def cool?(program) reply = process_command(program, 'cool') reply.chomp.match(/yes_i_am_cool$/) end end program = IO.popen("./program", "w+") a = Test.new(program) (...)in `write': not opened for writing (IOError) As we can see, there is an error when I try to send a message to the sub-process because of the closure of the stream (http://ruby-doc.org/core/classes/IO.html#M002289). I believe that it is not possible to open again a closed stream. Do you think a good way is to don't close the stream (except at the very end of the Ruby script)? or should it be better to change the current code structure? Thanks again for any help. -- Posted via http://www.ruby-forum.com/.