Class: Rtimeout

Inherits:
Object
  • Object
show all
Defined in:
lib/rtimeout.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, command) ⇒ Rtimeout

Returns a new instance of Rtimeout.



5
6
7
8
9
# File 'lib/rtimeout.rb', line 5

def initialize(timeout, command)
  @timeout = timeout.to_i
  @command = command
  @pid, _, @io, _ = Open4.popen4(command)
end

Class Method Details

.read(timeout, command) ⇒ Object



24
25
26
# File 'lib/rtimeout.rb', line 24

def self.read(timeout, command)
  new(timeout, command).read
end

Instance Method Details

#readObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rtimeout.rb', line 11

def read
  while result = IO.select([@io], nil, nil, @timeout)
    out = @io.read(1)
    break if out.nil?
    $stdout.print out
  end

  if result.nil?
    system "kill -9 #{@pid}"
    $stderr.puts "rtimeout: Command `#{@command}` timed out." 
  end
end