Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: How to flush stdin to popen3 without closing it? Date: Sun, 26 Feb 2017 18:42:58 +0100 Lines: 43 Message-ID: References: <8760jwkiyv.fsf@filestore.home.gustad.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Or1joebK7xZqVW/rJGEF0AIiZ1RKCyG7W7/1AN2f0fFmI1RMc= Cancel-Lock: sha1:yDBvtN7nFJX4HORxIIGX/zdfSV4= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 In-Reply-To: <8760jwkiyv.fsf@filestore.home.gustad.com> Xref: csiph.com comp.lang.ruby:7339 On 26.02.2017 17:36, Petter Gustad wrote: > > It seems like stdin must be closed for popen3 to send stdin to the > sub-process. How can I do something like this: > > require 'open3' > > stdin, stdout, stderr, wait_thr = Open3.popen3("dc") > stdin.puts "1 2 + p" > stdin.flush # does not not help > stdin.close # without this it will just hang on the read > res = stdout.read > puts "1+2 is #{res}" > #stdin.reopen # is it possible to re-open the stdin somehow? > stdin.puts "#{res} 2 * p" > stdin.close > res = stdout.read > puts "(1+2)*2 is #{res}" > ... > > Is there a way to flush or re-open stdin? Is there some other variation > of popen3 which can be used? You are missing one important point: stdout.read must block until dc closes its output because there is no limit set on the read amount. And that will only happen if dc's input has been closed because, obviously, dc needs to be able to write results as long as input is coming. If you replace that line with res = stdout.gets it will work the way you expect. You could also use stdout.sync=true to avoid to have to use #flush all the time. http://ruby-doc.org/core-2.4.0/IO.html#method-i-sync-3D Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/