简体   繁体   English

多个HTTP请求-Rails

[英]Multiple HTTP request - Rails

My application checks a number of domains to see if they are valid (approx 100). 我的应用程序检查了多个域,以查看它们是否有效(大约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. 因此,我不必等待域1响应即可请求域2。

I have managed this in PHP using curl_multi to run the requests in parallel. 我已经在PHP中使用curl_multi来并行运行请求,对此进行了管理。 Is there a similar thing I can do in Rails? 我可以在Rails中做类似的事情吗?

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 t1 = Thread.new {func1()} t2 = Thread.new {func1()} t1.join t2.join

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

This is emulated parallelism and i dont see why it wont be faster than b4 这是模拟并行性,我不明白为什么它不会比b4快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM