Created
July 26, 2012 10:09
-
-
Save jonleighton/3181338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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