Ruby on rails Ruby Timeout::Timeout不';不触发异常并且不';我不返回记录的内容

Ruby on rails Ruby Timeout::Timeout不';不触发异常并且不';我不返回记录的内容,ruby-on-rails,ruby,timeout,rescue,Ruby On Rails,Ruby,Timeout,Rescue,我有一段代码: 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 Exce

我有一段代码:

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 异常消息:执行已过期 异常回溯:/***/***/.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
打印“请给我打印一些东西”

尝试上面的基本代码。如果这样做有效,则说明平台中存在问题。请根据以下内容搜索:

如果块执行终止 在秒过去之前,它 返回true。如果没有,则终止 执行并引发异常 (默认为超时::错误)

这意味着它只有在成功时才返回true,否则将不会设置变量(即它为nil而不是false)


就你的例子而言,我肯定是在超时,并进入救援部分…

问题在于
平台。search
正在捕获
超时#超时引发的异常

您可以通过将内部代码包装到另一个线程——YMMV中来解决这个问题


你绝对是对的。我不知道为什么我没有检查platform.search。实际上,搜索是在营救。。例外。。。非常感谢!我最近在这里描述并修复了这个问题:超时确实很糟糕,但这个线程化方法工作得很好,谢谢,或者试试软超时
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