dam*_*ser 3 methods timeout ruby-on-rails-3
我有一些做很多事情的任务,其中一些可以"阻塞",因为它调用外部API.
我的问题:可以确定RailsThread在方法中"停留"多长时间?等等,如果花费太长时间就会中断或重新加载.问题是没有错误,所以我不能做任何像救援这样的事情.
我想做的伪代码:
def aMethod
#doSomethingThatCanBlock
if takeMoreThan1000ms
#reloadMethod or break
end
end
Run Code Online (Sandbox Code Playgroud)
require 'timeout'
def a_method(iterations)
Timeout::timeout(1) do # 1 second
iterations.times { |i| print("#{i} "); sleep(0.1) }
end
rescue Timeout::Error
print("TIMEOUT")
ensure
puts
end
Run Code Online (Sandbox Code Playgroud)
一个例子:
irb(main):012:0> a_method(3)
0 1 2
=> 3
irb(main):013:0> a_method(30)
0 1 2 3 4 5 6 7 8 9 TIMEOUT
=> nil
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1237 次 |
| 最近记录: |