gpt4 book ai didi

ruby - 如果在 `x` 秒内没有用户输入则超时

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:45 25 4
gpt4 key购买 nike

如果在 x 时间内没有输入用户输入,我将如何在 Ruby 中编写一段代码,该代码将超时或退出?

我没有完成一半的脚本能更好地表达我的问题,甚至没有伪代码概念算法。

最佳答案

您可以使用 Timeout包含在标准库中的模块。如果您想拯救它,它会在超时时引发Timeout::Error

require 'timeout'
x = 10
begin
status = Timeout::timeout(x) {
printf "Input: "
gets
}
puts "Got: #{status}"
rescue Timeout::Error
puts "Input timed out after #{x} seconds"
end

关于ruby - 如果在 `x` 秒内没有用户输入则超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809769/

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