Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 防止连接到URL时超时_Ruby_Ruby 1.8 - Fatal编程技术网

Ruby 防止连接到URL时超时

Ruby 防止连接到URL时超时,ruby,ruby-1.8,Ruby,Ruby 1.8,我想在下面的代码中看到使用Benchmark访问url所花费的时间。我也尝试在没有基准的情况下做同样的事情。也就是说,得到测试开始和结束时的时间,减去这两个时间得到时间。这两种方法都以相同的超时错误结束 require 'open-uri' require 'benchmark' response = nil puts "opening website with benchmark..." puts Benchmark.measure{ response = open('http://my

我想在下面的代码中看到使用
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\u timeout
选项,以秒为单位指定,例如

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

用于连接的
BufferedIO
类具有一个

Net::HTTP
类提供了一个

不幸的是,该请求的
openuri
方式无法在请求之前获得该设置,也无法轻松覆盖默认值
60

您可能需要自己使用
Net::HTTP

link = URI.parse(url)
request = Net::HTTP::Get.new(link.path)
response = Net::HTTP.start(link.host, link.port) {|http|
  http.read_timeout = 100 #Default is 60 seconds
  http.request(request)
}
从……偷来的代码


编辑:或升级到1.9,该版本支持
:read_timeout=x
选项

谢谢。如何将超时设置为无穷大?无法从您提供的链接中快速获得答案。超时=-1???我收到错误-
语法错误,意外的“:”,期望“')响应=打开(url,读取超时:300)
@BoratSagdiyev。。。您可能应该为您的Ruby版本使用正确的映射语法。我会编辑我的答案,但是能够从代码样本中推断是一项很有价值的技能。谢谢Dave。我刚刚意识到我不能使用这个参数,因为我有ruby 1.8。有没有其他办法?谢谢错误-
检查选项:无法识别的选项:读取超时(ArgumentError)
@BoratSagdiyev-Oy,我在比较版本时误读了我使用的版本-我的错!将删除答案--对此表示抱歉。