Ruby Timeout::timeout 不会引发异常,也不会返回记录的内容

发布于 2024-10-11 01:57:56 字数 517 浏览 6 评论 0原文

我有这段代码:

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

所以我天真地认为我的调用超时了,但是“请打印一些东西”永远不会被打印并且< code>complete_results 假设是超时状态返回值(true 或 false,如文档中所述),绝对不是布尔值。

我做错了什么吗?

I have this piece of code:

begin
  complete_results = Timeout.timeout(4) do      
    results = platform.search(artist, album_name)
  end
rescue Timeout::Error
  puts 'Print me something please'
end 

I then launch the method containing this code, and well, here is the beginning of a stack trace:

Exception message :  execution expired
Exception backtrace :  /***/****/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:64:i

So I naively thinks that my call timed out, but 'Print me something please' is never printed and complete_results which is suppose to be the timeout status return value (either true or false, as mentioned in the documentation), is definitively not a boolean.

Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静水深流 2024-10-18 01:57:56

您的代码是正确的

require 'timeout'
begin
  complete_results = Timeout.timeout(1) do      
   sleep(2)
  end
rescue Timeout::Error
  puts 'Print me something please'
end

,确实打印出“请给我打印一些东西”。

尝试上面的基本代码。如果有效,则说明 platform.search 中存在问题。

Your code is correct

require 'timeout'
begin
  complete_results = Timeout.timeout(1) do      
   sleep(2)
  end
rescue Timeout::Error
  puts 'Print me something please'
end

does print out "print me something please".

Try the basic code as above. If that works, you have an issue in platform.search.

深居我梦 2024-10-18 01:57:56

问题是 platform.search 正在捕获 Timeout#timeout 抛出的异常

您可以通过将内部代码包装在另一个线程中来解决这个问题 - 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 

The problem is that platform.search is catching the exception that Timeout#timeout throws.

You can get around this by wrapping your inner code in another thread -- 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 
暖风昔人 2024-10-18 01:57:56

根据文档

如果块执行终止
在几秒过去之前,它
返回真。如果没有,则终止
执行并引发异常
(默认为 Timeout::Error)

这意味着只有成功才返回 true,否则不会设置变量(即 nil NOT false)。

就你的例子而言,这对我来说绝对是超时并进入救援部分......

According to the documentation:

If the block execution terminates
before sec seconds has passed, it
returns true. If not, it terminates
the execution and raises exception
(which defaults to Timeout::Error)

This means it only returns true if it's successful, otherwise the variable will not be set (ie it's nil NOT false).

As far as your example goes, it's definitely timing out for me and getting to the rescue part...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文