Note that there are some explanatory texts on larger screens.

plurals
  1. PODiscussion with a sub-process, using Ruby with IO and threading
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>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) &amp;&amp; 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! </code></pre> <blockquote> <p>(...) in `write': not opened for writing (IOError)</p> </blockquote> <p>As we can see, this error occur during is_cool? test (as explained at: <a href="http://ruby-doc.org/core/classes/IO.html#M002289" rel="nofollow noreferrer">http://ruby-doc.org/core/classes/IO.html#M002289</a>).</p> <p>But if I try to comment in process_command method the line:</p> <pre><code># sub_process.close_write </code></pre> <p>the script seems to sleep... infinitely :s</p> <p>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).</p> <p>How changes can I do over the current code in order to solve this problem?</p> <p><strong>Edit</strong>: in other words, I would like to establish a such communication (according to a given protocol):</p> <pre><code>Parent message: Child answer: -------------- ------------ 'are u ready?' 'yes_i_am_ready' 'are u cool?' 'yes_i_am_cool' 'Hello' 'foo' 'my name is ...' 'bar' </code></pre> <p>Many thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload