views:

36

answers:

2

Hi All. When reading data from a potentially slow website, I want to ensure that get_response can not hang, and so added a timer to timeout after x seconds. So far, so good. I then read http://ph7spot.com/musings/system-timer which illustrates that in certain situations timer.rb doesn't work due to ruby's implementation of threads.

Does anyone know if this is one of these situations?

url = URI.parse(someurl)

begin
    Timeout::timeout(30) do 
        response = Net::HTTP.get_response(url)
        @responseValue = CGI.unescape(response.body)
    end 
rescue Exception => e 
    dosomething
end
A: 

well, first of all Timeout is not a class defined in Rails but in Ruby, second, Timeout is not reliable in cases when you make system calls.

Ruby uses what it's so called Green Threads. Let's suppose you have 3 threads, you think all of them will run in parallel but if one of the threads makes a syscall all the rest of the threads will be blocked until the syscall finishes, in this case Timeout won't work as expected, so it's always better to use something reliable like SystemTimer.

raf
I understand the green thread blocking issue. My question is whether Net::HTTP.get_response is actually making a system call which will block and so prevent the timer (as in timeout.rb)from working. I suspect the answer is yes, but what has surprised me is that generally this doesn't seem to be viewed as a concern. No code examples seem to use the systemtimer gem. Or is there something that I have missed?
Frank
as far as I know the answer is yes, Net::HTTP.get_response make a system call, so it blocks.
raf
A: 

The answer is no.

Frank