红宝石超时::超时不火的异常,不返回什么记录
问题描述:
我有这段代码:红宝石超时::超时不火的异常,不返回什么记录
begin
complete_results = Timeout.timeout(4) do
results = platform.search(artist, album_name)
end
rescue Timeout::Error
puts 'Print me something please'
end
我再推出包含此代码的方法,和好,这里是开始堆栈轨迹:
Exception message : execution expired Exception backtrace : /***/****/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:64:i
所以我天真地认为,我的电话超时。但是“请打印我的东西”绝不会被打印出来,而complete_results
假设为超时状态返回值(无论是true还是false,正如文档中提到的那样),肯定不是布尔值。
我做错了什么?
答
你的代码是正确的
require 'timeout'
begin
complete_results = Timeout.timeout(1) do
sleep(2)
end
rescue Timeout::Error
puts 'Print me something please'
end
并打印出 “打印我的东西,请”。
试试上面的基本代码。如果可行,你在platform.search
有问题。
答
如果块执行秒秒之前终止 已经过去,它 返回true。如果不是,则终止 执行并产生异常 (默认为超时::错误)
这意味着只有当它的成功返回true,否则变量将不会被设置(即它是无不是假的)。
至于你的榜样去,它肯定超时我和获得救助的一部分...
答
的问题是,platform.search
被捕获的异常Timeout#timeout throws
。
你可以通过在另一个线程--YMMV中包装你的内部代码来解决这个问题。
begin
complete_results = Timeout.timeout(4) do
Thread.new{ results = platform.search(artist, album_name) }.value
end
rescue Timeout::Error
puts 'Print me something please'
end
+1
超时::超时再次吸... – rogerdpack 2012-09-19 14:08:03
你确实是对的。我不知道为什么我没有检查platform.search。其实,搜索是抢救..例外...非常感谢! – Pasta 2011-01-09 02:42:15