Method: Async::Reactor#timeout

Defined in:
lib/async/reactor.rb

#timeout(duration) ⇒ Object

Invoke the block, but after the timeout, raise TimeoutError in any currenly blocking operation.

Parameters:

  • duration (Integer)

    The time in seconds, in which the task should complete.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/async/reactor.rb', line 196

def timeout(duration)
	backtrace = caller
	task = Fiber.current
	
	timer = self.after(duration) do
		if task.alive?
			error = TimeoutError.new("execution expired")
			error.set_backtrace backtrace
			task.resume error
		end
	end
	
	yield
ensure
	timer.cancel if timer
end