open-uri timeout

Discussion in 'Ruby' started by Jeffrey, Aug 16, 2006.

  1. Jeffrey

    Jeffrey Guest

    I love open-uri, but does anyone know how to configure the time-out
    before open() will throw a Timeout::Error? I'd like to make it
    shorter.

    I'm trying to download some documents from a server that sometimes
    works fine, but other times seems to ignore HTTP requests entirely. If
    a download doesn't begin after a few seconds, I want to give up on the
    request and try again. Ideas are welcome.
     
    Jeffrey, Aug 16, 2006
    #1
    1. Advertising

  2. Jeffrey

    Guest

    On Thu, 17 Aug 2006, Jeffrey wrote:

    > I love open-uri, but does anyone know how to configure the time-out
    > before open() will throw a Timeout::Error? I'd like to make it
    > shorter.
    >
    > I'm trying to download some documents from a server that sometimes
    > works fine, but other times seems to ignore HTTP requests entirely. If
    > a download doesn't begin after a few seconds, I want to give up on the
    > request and try again. Ideas are welcome.


    the simple way is

    require 'timeout'

    retries = 42

    begin
    Timeout::timeout(s){
    open('http://a.url.com') do |f|
    # ... stuff with f
    end
    }
    rescue Timeout::Error
    retries -= 1
    if retry > 0
    sleep 0.42 and retry
    else
    raise
    end
    end

    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
     
    , Aug 16, 2006
    #2
    1. Advertising

  3. Jeffrey

    Jeffrey Guest

    wrote:
    > On Thu, 17 Aug 2006, Jeffrey wrote:
    >
    > > I love open-uri, but does anyone know how to configure the time-out
    > > before open() will throw a Timeout::Error? I'd like to make it
    > > shorter.


    > the simple way is
    >
    > require 'timeout'
    >
    > retries = 42
    >
    > begin
    > Timeout::timeout(s){
    > open('http://a.url.com') do |f|
    > # ... stuff with f
    > end
    > }
    > rescue Timeout::Error
    > retries -= 1
    > if retry > 0
    > sleep 0.42 and retry
    > else
    > raise
    > end
    > end


    Thanks, that's great. It seems to be working.
     
    Jeffrey, Aug 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris
    Replies:
    0
    Views:
    6,383
    Simon Harris
    May 10, 2005
  2. Stanimir Stamenkov
    Replies:
    1
    Views:
    2,476
    Stanimir Stamenkov
    Aug 17, 2005
  3. Pavel
    Replies:
    2
    Views:
    1,652
    Peter Flynn
    Aug 4, 2004
  4. Jay 99
    Replies:
    2
    Views:
    192
    Jay 99
    Apr 4, 2009
  5. Luis Some

    Timeout exception open-uri

    Luis Some, Aug 19, 2010, in forum: Ruby
    Replies:
    2
    Views:
    520
    Luis Some
    Aug 19, 2010
Loading...

Share This Page