Ruby Net::HTTP time out

前端 未结 2 1659
灰色年华
灰色年华 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 17:46

    For Ruby 1.8 I used this to solve my time-out issues. Extending the Net::HTTP class in my code and re-initialized with default parameters including an initialization of my own read_timeout should keep things sane I think.

    require 'net/http'
    
    # Lengthen timeout in Net::HTTP
    module Net
        class HTTP
            alias old_initialize initialize
    
            def initialize(*args)
                old_initialize(*args)
                @read_timeout = 5*60     # 5 minutes
            end
        end
    end
    
    0 讨论(0)
  • 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 `<main>'
    

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

    0 讨论(0)
提交回复
热议问题