From d3cf61b05d9507e7b6ea5a1a1192e107a8612049 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 13 Dec 2015 12:19:00 +0000 Subject: switch to exception-free non-blocking I/O Ruby 2.3 will have `exception: false' support in socket-related classes. Additionally, 2.3 will implement the existing IO#*_nonblock methods more efficiently than before by avoiding the hash allocation necessary for keywords. For users on older Rubies, we'll continue supporting them with compatibility wrappers; even Ruby 1.9.3 users (for now). --- lib/dtas/process.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/dtas/process.rb') diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb index f5f9a9e..8c46d9d 100644 --- a/lib/dtas/process.rb +++ b/lib/dtas/process.rb @@ -4,6 +4,7 @@ require 'io/wait' require 'shellwords' require_relative '../dtas' require_relative 'xs' +require_relative 'nonblock' # process management helpers module DTAS::Process # :nodoc: @@ -86,12 +87,13 @@ module DTAS::Process # :nodoc: cmd, opts = env, cmd env = {} end - r, w = IO.pipe + buf = '' + r, w = DTAS::Nonblock.pipe opts = opts.merge(out: w) r.binmode no_raise = opts.delete(:no_raise) if err_str = opts.delete(:err_str) - re, we = IO.pipe + re, we = DTAS::Nonblock.pipe re.binmode opts[:err] = we end @@ -105,12 +107,11 @@ module DTAS::Process # :nodoc: begin readable = IO.select(want.keys) or next readable[0].each do |io| - begin - want[io] << io.read_nonblock(2000) - rescue Errno::EAGAIN - # spurious wakeup, bytes may be zero - rescue EOFError - want.delete(io) + case rv = io.read_nonblock(2000, buf, exception: false) + when :wait_readable # spurious wakeup, bytes may be zero + when nil then want.delete(io) + else + want[io] << rv end end end until want.empty? -- cgit v1.2.3-24-ge0c7