Skip to content

Instantly share code, notes, and snippets.

@mitsuru793
Created October 12, 2015 06:15
Show Gist options
  • Select an option

  • Save mitsuru793/9ff0e65ffa08d949d1db to your computer and use it in GitHub Desktop.

Select an option

Save mitsuru793/9ff0e65ffa08d949d1db to your computer and use it in GitHub Desktop.
rubyのtimeout関数を試してみる
require 'timeout'
require 'open-uri'
TIMEOUT_SECOND = 3
# 3秒以内にdo~endの処理が完了しないとエラーになる
timeout(TIMEOUT_SECOND) do
puts "task1 done"
end
# => task1 done
# sleepは指定した秒数待機する。
begin
timeout(TIMEOUT_SECOND) do
sleep TIMEOUT_SECOND + 1
puts "task2 done"
end
rescue => e
puts e
puts e.message
end
# => execution expired
# execution expired
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment