Method: Async::Scheduler#with_timeout
- Defined in:
- lib/async/scheduler.rb
#with_timeout(duration, exception = TimeoutError, message = "execution expired", &block) ⇒ Object
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.
268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/async/scheduler.rb', line 268 def with_timeout(duration, exception = TimeoutError, = "execution expired", &block) fiber = Fiber.current timer = @timers.after(duration) do if fiber.alive? fiber.raise(exception, ) end end yield timer ensure timer.cancel if timer end |