【问题标题】:rescue Timeouts with SystemTimer使用 SystemTimer 抢救超时
【发布时间】:2011-02-11 19:09:56
【问题描述】:

我正在使用 SystemTimer gem 来处理超时问题。 https://github.com/ph7/system-timer

我找不到在超时时捕获异常的方法

begin
  SystemTimer.timeout_after(10.seconds) do
    # facebook api
    rest_graph.fql(query)
  end
rescue RestGraph::Error::InvalidAccessToken
  return nil
rescue Timeout::Error
  # never executed
end

但最后一个 Exception Timeout::Error 从未触发。

【问题讨论】:

  • [12:19][~]$ ruby​​ -v ruby​​ 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

标签: ruby timeout


【解决方案1】:

为什么不使用 Timeout,它是 1.9.2 附带的,旨在做到这一点?

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

【讨论】:

    【解决方案2】:

    试试这个:(根据您的链接)

    class TimedOut < StandardError
    end
    
    begin
      SystemTimer.timeout_after(10.seconds, TimedOut) do
        # ...
      end
    rescue TimedOut
      # ...
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2011-03-16
      相关资源
      最近更新 更多