首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby -超时不起作用

Ruby -超时不起作用
EN

Stack Overflow用户
提问于 2016-11-04 21:48:55
回答 1查看 276关注 0票数 0

我希望这段代码的运行时间不超过5秒:

代码语言:javascript
复制
require 'httpi'
require 'timeout'

puts Time.new
begin
    request,response=nil,nil
    Timeout::timeout(5){
        request=HTTPI::Request.new(url: "http://example.com")
        response=HTTPI.get(request)
    }
rescue 
    puts "except: #{$!}"
ensure
    puts Time.new
end

但这是我得到的输出:

代码语言:javascript
复制
2016-11-04 09:44:55 -0400
D, [2016-11-04T09:44:55.916557 #2476] DEBUG -- : HTTPI GET request to example.com (net_http)
except: execution expired
2016-11-04 09:45:16 -0400

我假设NET的默认HTTP超时是20秒,所以Timeout::timeout只是允许代码运行它想运行的时间。为什么?

EN

回答 1

Stack Overflow用户

发布于 2016-11-06 23:09:36

正如你所看到的hereherehere,Ruby的Timeout模块是出了名的有一些问题。

除非非常必要,否则您应该考虑不使用此模块。

相反,您可以使用HTTPI提供的read_timeout和/或open_timeout选项。

代码语言:javascript
复制
request = HTTPI::Request.new(url: "http://example.com")
request.open_timeout = 5 # seconds
request.read_timeout = 5 # seconds
response = HTTPI.get(request)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40424352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档