gpt4 book ai didi

ruby - 防止连接到 URL 时超时

转载 作者:数据小太阳 更新时间:2023-10-29 07:40:52 26 4
gpt4 key购买 nike

我想在下面的代码中使用 Benchmark 查看访问 url 所花费的时间。我也尝试在没有基准测试的情况下做同样的事情。即获取测试开始时间和测试结束时间,将两者相减得到时间。两种方法都以相同的超时错误结束。

require 'open-uri'
require 'benchmark'

response = nil
puts "opening website with benchmark..."
puts Benchmark.measure{
response = open('http://mywebsite.com')
}

puts "Done !"
status = response.status
puts status

错误:

opening website with benchmark...
C:/ruby/lib/ruby/1.8/timeout.rb:64:in `rbuf_fill': execution expired (Timeout::Error)
from C:/ruby/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from C:/ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from C:/ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from C:/ruby/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
from C:/ruby/lib/ruby/1.8/net/http.rb:2017:in `read_new'
from C:/ruby/lib/ruby/1.8/net/http.rb:1051:in `request'
from C:/ruby/lib/ruby/1.8/open-uri.rb:248:in `open_http'
from C:/ruby/lib/ruby/1.8/net/http.rb:543:in `start'
from C:/ruby/lib/ruby/1.8/open-uri.rb:242:in `open_http'
from C:/ruby/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
from C:/ruby/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from C:/ruby/lib/ruby/1.8/open-uri.rb:162:in `catch'
from C:/ruby/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
from C:/ruby/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from C:/ruby/lib/ruby/1.8/open-uri.rb:518:in `open'
from C:/ruby/lib/ruby/1.8/open-uri.rb:30:in `open'
from C:/code/test.rb:7
from C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
from C:/code/test.rb:6

当我尝试在我的浏览器中连接到此 URL 时,它始终需要大约 2-3 分钟才能访问。

我搜索了谷歌,但没有找到对我的问题有用的答案。我知道我必须更改某项的超时设置,但无法弄清楚是哪一个。有人可以帮忙吗?

最佳答案

使用 :read_timeout 选项,以秒为单位指定,例如,

open('foo.com', :read_timeout => 10)

http://ruby-doc.org/stdlib-1.8.7/libdoc/open-uri/rdoc/OpenURI/OpenRead.html

关于ruby - 防止连接到 URL 时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com