13 - adding a timeout to a long-running operation.rb
来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 24 行
RB
24 行
# This code will sleep forever... OR WILL IT?require 'timeout'before = Time.nowbegin status = Timeout.timeout(5) { sleep }rescue Timeout::Error puts "I only slept for #{Time.now-before} seconds."end# I only slept for 5.035492 seconds.#---def count_for_five_seconds $counter = 0 begin Timeout::timeout(5) { loop { $counter += 1 } } rescue Timeout::Error puts "I can count to #{$counter} in 5 seconds." endendcount_for_five_seconds# I can count to 2532825 in 5 seconds.$counter # => 2532825#---
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?