Timeout ::循环中的错误异常

时间:2014-05-06 06:46:22

标签: 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.kill('TERM', pid)
end

问题是,一旦捕获了Timeout :: Error异常,就会跳过该块,并且循环几乎不做任何事情。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

rescue以后,您需要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

{{1}}