1 min read

TIL: Ruby's Built-in Timeout

Explicitly timeout long-running tasks

Ruby has built-in timeout functionality in the standard library. Useful for killing tasks which might be long-running (or never-ending).

require 'timeout'

result = Timeout::timeout(5) do
  sleep(6)
end

Here a Timeout::Error will be raised. If the timeout isn't triggered, the block's result will be stored in results.