class Async::Scheduler
def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)
@parameter message [String] The message to pass to the exception.
@parameter exception [Class] The exception class to raise.
@parameter duration [Numeric] The time in seconds, in which the task should complete.
@asynchronous May raise an exception at any interruption point (e.g. blocking operations).
@public Since *Async v1*.
Invoke the block, but after the specified timeout, raise {TimeoutError} in any currenly blocking operation. If the block runs to completion before the timeout occurs or there are no non-blocking operations after the timeout expires, the code will complete without any exception.
def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block) fiber = Fiber.current timer = @timers.after(duration) do if fiber.alive? fiber.raise(exception, message) end end yield timer ensure timer&.cancel! end