0

I want to be able to do something like this in Ruby:

(new[]{"http://google.com", "http://facebook.com", "http://microsoft.com", "http://twitter.com", "http://stackoverflow.com"})
.AsParallel()
.Select(q => WebRequest.Create(q).GetResponse())

This also should preserve order of elements and use deferred execution. Are there any options available?

Vasaka
  • 579
  • 6
  • 17
  • The only real solution I found so far is manually managing threads in a loop, which does not provide deferred execution and pretty low level. – Vasaka Oct 15 '12 at 15:50
  • You're going to want to put that solution in the question because SO is not properly used as a board to write code for other people. However, if you've tried something it shows that you put in some effort and people are much more apt to help. – Mike Perrenoud Oct 15 '12 at 15:52

1 Answers1

1

The Ruby curb gem, which is a wrapper around the curl library, has a "multi" mode that can fetch a number of URLs in parallel.

From the example:

# Make multiple GET requests
easy_options = { :follow_location => true }
multi_options = { :pipeline => true }

Curl::Multi.get('url1','url2','url3','url4','url5', easy_options, multi_options) do |easy|
  # do something interesting with the easy response
  puts easy.last_effective_url
end
tadman
  • 208,517
  • 23
  • 234
  • 262
  • As far as I am understand this is not common solution for managing collections in asynchronous way? – Vasaka Oct 15 '12 at 16:07
  • There are many different ways of doing asynchronous networking in Ruby such as threads or fibers or, in this case, an application specific implementation. – tadman Oct 15 '12 at 16:17