gpt4 book ai didi

ruby - 超时::循环中的错误异常

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:50 34 4
gpt4 key购买 nike

我在循环中有这段 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 异常被捕获, block 将被跳过并且循环几乎什么都不做。我该如何解决这个问题?

最佳答案

Timeout::Error is not a standard error以来,您需要专门针对Timeout:Errorrescue :

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

关于ruby - 超时::循环中的错误异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23487681/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com