Class: IO

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

Overview

Monkey Patch timeout support into the IO class

Instance Method Summary collapse

Instance Method Details

#each_with_timeout(pid, seconds, sep_string = $/) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ffmpeg/io_monkey.rb', line 15

def each_with_timeout(pid, seconds, sep_string=$/)
  last_update = Time.now

  current_thread = Thread.current
  check_update_thread = Thread.new do
    loop do
      sleep 0.1
      if last_update - Time.now < -seconds
        current_thread.raise Timeout::Error.new('output wait time expired')
      end
    end
  end

  each(sep_string) do |buffer|
    last_update = Time.now
    yield buffer
  end
rescue Timeout::Error
  if RUBY_PLATFORM =~ /(win|w)(32|64)$/
    Process.kill(1, pid)
  else
    Process.kill('SIGKILL', pid)
  end
  raise
ensure
  check_update_thread.kill
end