Ruby 超时::循环中的错误异常

Ruby 超时::循环中的错误异常,ruby,exception,timeout,Ruby,Exception,Timeout,我将这段ruby代码放在一个循环中: pid = Process.spawn("my_process_command") begin Timeout.timeout(16) do `my_timeout_command` Process.wait(pid) end rescue system("clear") puts 'Process not finished in time, killing it' Process.ki

我将这段ruby代码放在一个循环中:

pid = Process.spawn("my_process_command")
begin
    Timeout.timeout(16) do
        `my_timeout_command`
        Process.wait(pid)
    end
rescue
    system("clear")
    puts 'Process not finished in time, killing it'
    Process.kill('TERM', pid)
end

问题是,一旦捕获到Timeout::Error异常,就会跳过块,而循环实际上什么也不做。如何解决此问题?

您需要
救援
专门针对
超时:错误
,因为:


但不是[这](“假设超时::错误是默认值?超时(秒,klass=nil)在块中执行操作,如果完成时间超过秒,则引发错误。如果块未能在秒内终止,则引发klass异常类。忽略将使用默认值Timeout::error。@user3606584 Yes,这意味着您可以调用
Timeout.Timeout(16,ProcessNotFinishedException)
ProcessNotFinishedException
将引发,而不是
Timeout::Error
pid = Process.spawn("my_process_command")
begin
    Timeout.timeout(16) do
        `my_timeout_command`
        Process.wait(pid)
    end
rescue Timeout::Error
    system("clear")
    puts 'Process not finished in time, killing it'
    Process.kill('TERM', pid)
end