views:

133

answers:

1

My application checks a number of domains to see if they are valid (approx 100). I have the following code to check a single domain:

def self.test_url uri, limit = 10
    if limit == 0 
        return get_error_messages("001")
    end
    begin 
        url = URI.parse(uri)
        response = Net::HTTP.start(url.host, url.port).request_head('/')  
    rescue SocketError => e 
        return get_error_messages("002")
    end
    case response
        when Net::HTTPRedirection then test_url(response['location'], limit - 1)
        else return get_error_messages(response.code)
    end
end

The code checks for the response code while taking into account redirects. This works fine. The only problem I have is when I put this in a loop I want it to run in parallel. So I don't have to wait for domain 1 to respond before I can request domain 2.

I have managed this in PHP using curl_multi to run the requests in parallel. Is there a similar thing I can do in Rails?

+1  A: 

One suggestion is to divide your list of domains in two or more array parts

then start different threads for each of them

t1=Thread.new{func1()} t2=Thread.new{func1()} t1.join t2.join

http://www.tutorialspoint.com/ruby/ruby_multithreading.htm

This is emulated parallelism and i dont see why it wont be faster than b4

Jet Abe