Ruby Net::HTTP time out

前端 未结 2 1664
灰色年华
灰色年华 2020-12-09 17:09

I\'m trying to write my first Ruby program, but have a problem. The code has to download 32 MP3 files over HTTP. It actually downloads a few, then times-out.

I tried

2条回答
  •  一个人的身影
    2020-12-09 18:09

    Your timeout isn't in the code you set the timeout for. It's here, where you use open-uri:

    open("http://www.tubeminator.com/ajax.php?function=downloadvideo&url=http%3A%2F%2Fwww.vimeo.com%2F" + link[0],
    

    You can set a read timeout for open-uri like so:

    #!/usr/bin/ruby1.9
    
    require 'open-uri'
    
    open('http://stackoverflow.com', 'r', :read_timeout=>0.01) do |http|
      http.read
    end
    
    # => /usr/lib/ruby/1.9.0/net/protocol.rb:135:in `sysread': \
    # => execution expired (Timeout::Error)
    # => ...
    # =>         from /tmp/foo.rb:5:in `
    '

    :read_timeout is new for Ruby 1.9 (it's not in Ruby 1.8). 0 or nil means "no timeout."

提交回复
热议问题