【发布时间】:2015-02-22 02:21:36
【问题描述】:
我在 Ruby 中发现了 Timeout 模块,并想对其进行测试。我在http://ruby-doc.org/stdlib-2.1.1/libdoc/timeout/rdoc/Timeout.html查看了他们的官方源代码
这是我的代码
require 'timeout'
require 'benchmark'
numbers = [*1..80]
Timeout::timeout(5) { numbers.combination(5).count }
=> 24040016
我做了一些基准测试,得到了以下结果。
10.828000 0.063000 10.891000 11.001676
根据文档,如果该块未在 5 秒内执行,则此方法应该返回异常。如果在时间范围内执行,则返回代码块的结果
对于它的价值,我尝试了 1 秒而不是 5 秒的超时,但我仍然得到返回代码块的结果。
这里是官方文档
timeout(sec, klass=nil)
Performs an operation in a block, raising an error if it takes longer than sec seconds to complete.
sec: Number of seconds to wait for the block to terminate. Any number may be used,
including Floats to specify fractional seconds. A value of 0 or nil will execute the
block without any timeout.
klass: Exception Class to raise if the block fails to terminate in sec seconds. Omitting
will use the default, Timeout::Error
我很困惑为什么这不起作用。
【问题讨论】: