Created
October 12, 2015 06:15
-
-
Save mitsuru793/9ff0e65ffa08d949d1db to your computer and use it in GitHub Desktop.
rubyのtimeout関数を試してみる
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' | |
| 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