open-uri timeout

J

Jeffrey

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.
 
A

ara.t.howard

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
 
J

Jeffrey

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top