Method: Concurrent.timeout

Defined in:
lib/concurrent/utility/timeout.rb

.timeout(seconds) ⇒ Object

Note:

This method is intended to be a simpler and more reliable replacement

Wait the given number of seconds for the block operation to complete.

to the Ruby standard library Timeout::timeout method.

Parameters:

  • seconds (Integer)

    The number of seconds to wait

Returns:

  • (Object)

    The result of the block operation

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/concurrent/utility/timeout.rb', line 19

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