title: asynchronous http request in ruby
body: <pre><code> require 'net/http'
  
  urls = [
    {'link' =&gt; 'http://www.google.com/'},
    {'link' =&gt; 'http://www.facebook.com/'},
   {'link' =&gt; 'http://www.yahoo.com/'}
  ]
  
  urls.each do |u|
    u['content'] = Net::HTTP.get( URI.parse(u['link']) )
  end
  
  print urls
  </code></pre>
  
  <p>This will work as procedural code.. I just want to hit a server, no issues about the order. How can i do that in ruby. One option is using threads.</p>
  
  <p>Here's an example using threads.</p>
  
  <pre><code>require 'net/http'
  
  urls = [
    {'link' =&gt; 'http://www.google.com/'},
    {'link' =&gt; 'http://www.facebook.com/'},
    {'link' =&gt; 'http://www.yahoo.com/'}
  ]
  
  urls.each do |u|
    Thread.new do
      u['content'] = Net::HTTP.get( URI.parse(u['link']) )
      puts "Successfully requested #{u['link']}"
  
      if urls.all? {|u| u.has_key?("content") }
        puts "Fetched all urls!"
        exit
      end
    end
  end
  </code></pre>
  
  <p>Any better solution..??</p>
  
  <p>PS:- i want to hit mixpanel, so that's why I just want to make a http call and dont wait for the response.</p>