<commit_before>if RUBY_VERSION =~ /1\.8/
	# Useful when `timeout.rb`, which, on M.R.I 1.8, relies on green threads, does not work consistently.
	begin
		require 'system_timer'
		FFMPEG::Timer = SystemTimer
	rescue LoadError
		require 'timeout'
		FFMPEG::Timer = Timeout
	end
else
	require 'timeout'
	FFMPEG::Timer = Timeout
end

require 'win32/process' if RUBY_PLATFORM =~ /(win|w)(32|64)$/

#
# Monkey Patch timeout support into the IO class
#
class IO
  def each_with_timeout(pid, seconds, sep_string=$/)
  	sleeping_queue = Queue.new
  	thread = nil

  	timer_set = lambda do
  	  thread = new_thread(pid) { FFMPEG::Timer.timeout(seconds) { sleeping_queue.pop } }
  	end

  	timer_cancel = lambda do
  		thread.kill if thread rescue nil
  	end

  	timer_set.call
  	each(sep_string) do |buffer|
  		timer_cancel.call
  		yield buffer
  		timer_set.call
  	end
  ensure
  	timer_cancel.call
  end

  private
  def new_thread(pid, &block)
  	current_thread = Thread.current
  	Thread.new do
  		begin
  			block.call
  		rescue Exception => e
  			current_thread.raise e
  			if RUBY_PLATFORM =~ /(win|w)(32|64)$/
  				Process.kill(1, pid)
  			else
  				Process.kill('SIGKILL', pid)
  			end
  		end
  	end
  end
end<commit_msg>My own implementation of io timeout hack.<commit_after>require 'timeout'
require 'thread'
require 'win32/process' if RUBY_PLATFORM =~ /(win|w)(32|64)$/

#
# Monkey Patch timeout support into the IO class
#
class IO
  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')
          Thread.current.kill
        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
  end
end
{'fraction_non_alphanumeric': 0.09728944820909971, 'fraction_numerical': 0.013552758954501452, 'mean_word_length': 2.863551401869159, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4367266', 'n_tokens_mistral': 791, 'n_tokens_neox': 748, 'n_words': 190}