如果没有用户输入“x”秒,则超时

时间:2016-04-23 10:46:14

标签: ruby timeout

如何在Ruby中编写一大块代码,如果在x时间内没有输入用户输入,那么会超时或退出?

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

2 个答案:

答案 0 :(得分:3)

您可以使用标准库中包含的Timeout模块。如果你想要rescue,它会在超时时提出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

backgroundWorker1.WorkerReportsProgress = true;

答案 1 :(得分:0)

require "timeout"

Timeout.timeout(x) do
  s = gets
  ...
end
相关问题