Ruby/Development/Timeout

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Adding a Timeout to a Long-Running Operation

require "timeout"
before = Time.now
begin
  status = Timeout.timeout(5) { sleep }
rescue Timeout::Error
  puts "I only slept for #{Time.now-before} seconds."
end



Count for five second

require "timeout"
def count_for_five_seconds
  $counter = 0
  begin
    Timeout::timeout(5) { loop { $counter += 1 } }
  rescue Timeout::Error
    puts "count to #{$counter} in 5 seconds."
  end
end
count_for_five_seconds
puts $counter