Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2016 10:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save domgetter/95b6f1372b9ec421f1523af4da175daa to your computer and use it in GitHub Desktop.
class Thread
class TimeoutError < StandardError; end
def self.timeout(n)
temp_queue =
work_thread = { temp_queue << {return: yield} } # wrapping in a hash guarantees uniqueness of output
timeout_thread = { sleep n; temp_queue << :timeout }
if (ret_val = temp_queue.pop) == :timeout # this pop is blocking, so it either waits on the yield or the timeout
raise TimeoutError
return ret_val[:return]
# This will either return "some val" or raise a Thread::Timeout
# Is raising an error a good idea? I don't know. The other
# options were to return nil or return a special Timeout object
# but I didn't like either of those, so I went with an error.
# It's not much trouble to wrap this in a begin, and it
# can be "dangerous", so I figured throwing an error was best.
Thread.timeout(1) do
sleep rand*2 # simulate work that takes 0 to 2 seconds
"some val"
# It actually feels pretty intuitive.
Thread.timeout(2) do
rescue Thread::TimeoutError
puts "operation took longer than 2 seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment