Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created July 26, 2012 10:09
Show Gist options
  • Select an option

  • Save jonleighton/3181338 to your computer and use it in GitHub Desktop.

Select an option

Save jonleighton/3181338 to your computer and use it in GitHub Desktop.
require 'timeout'
begin
Timeout.timeout(0.1) do
begin
sleep 0.2
# Timeout::ExitException gets raised on the main thread. We can rescue it to
# detect a timeout and add special handling.
rescue Timeout::ExitException
puts "Got a timeout"
raise
# This code will also run upon timeout (and also when we don't timeout, obviously)
ensure
puts "Ensure called"
end
end
# Outside of the timeout block, the ExitException is caught and a Timeout::Error
# is raised in its place.
rescue Timeout::Error
puts "Block timed out"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment