我的问题与如何解决超时问题(Ruby,Rails)有关.
以下是从超时中解救的常用方法:
def action
# Post using Net::HTTP
rescue Timeout::Error => e
# Do something
end
Run Code Online (Sandbox Code Playgroud)
我想确定在尝试连接到主机时是否引发了异常,或者是否在尝试从主机读取时引发了异常.这可能吗?
Mar*_*une 12
这是解决方案(在Ben的修复之后):
require "net/http"
http = Net::HTTP.new("example.com")
http.open_timeout = 2
http.read_timeout = 3
begin
http.start
begin
http.request_get("/whatever?") do |res|
res.read_body
end
rescue Timeout::Error
puts "Timeout due to reading"
end
rescue Timeout::Error
puts "Timeout due to connecting"
end
Run Code Online (Sandbox Code Playgroud)