Ruby Timeout::timeout 不会引发异常,也不会返回记录的内容
我有这段代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码是正确的
,确实打印出“请给我打印一些东西”。
尝试上面的基本代码。如果有效,则说明
platform.search
中存在问题。Your code is correct
does print out "print me something please".
Try the basic code as above. If that works, you have an issue in
platform.search
.问题是
platform.search
正在捕获Timeout#timeout 抛出的异常
。您可以通过将内部代码包装在另一个线程中来解决这个问题 - YMMV。
The problem is that
platform.search
is catching the exception thatTimeout#timeout throws
.You can get around this by wrapping your inner code in another thread -- YMMV.
根据文档:
这意味着只有成功才返回 true,否则不会设置变量(即 nil NOT false)。
就你的例子而言,这对我来说绝对是超时并进入救援部分......
According to the documentation:
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...