Sha256: d8ef3e2cc88e8727dbe23fa9662d048988d0796214741e2654fe4203cbf6b922

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 Bytes

Contents

require 'rbconfig'
require 'thread'

module Concurrent

  # Error raised when an operations times out.
  TimeoutError = Class.new(StandardError)

  # Wait the given number of seconds for the block operation to complete.
  #
  # @param [Integer] seconds The number of seconds to wait
  #
  # @return The result of the block operation
  #
  # @raise Concurrent::TimeoutError when the block operation does not complete
  #   in the allotted number of seconds.
  #
  # @note This method is intended to be a simpler and more reliable replacement
  # to the Ruby standard library `Timeout::timeout` method.
  def timeout(seconds)

    thread = Thread.new do
      Thread.current[:result] = yield
    end
    success = thread.join(seconds)

    if success
      return thread[:result]
    else
      raise TimeoutError
    end
  ensure
    Thread.kill(thread) unless thread.nil?
  end
  module_function :timeout
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
concurrent-ruby-0.6.0.pre.2 lib/concurrent/utility/timeout.rb