Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Last active December 29, 2015 23:19
Show Gist options
  • Select an option

  • Save invisiblefunnel/7742344 to your computer and use it in GitHub Desktop.

Select an option

Save invisiblefunnel/7742344 to your computer and use it in GitHub Desktop.
def timeout(seconds = nil)
timer_thread = Thread.start(Thread.current, Time.now) { |app_thread, start|
sleep(seconds)
app_thread.raise 'Execution timed out'
}
yield
ensure
timer_thread.kill
timer_thread.join
end
timeout(2.5) do
puts '!!!!'
end
timeout(2.5) do
sleep
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment