How to quickly load web pages in ruby? Parallelizing download?

I need to clear (using scrAPI) 400+ ruby ​​web pages, my actual code is very consistent:

data = urls.map {|url| scraper.scrape url }

Actually, the code is a little different (exception handling, etc.).

How can I do it faster? How can I parallelize downloads?

+3
source share
2 answers
th = []
data = []
dlock = Mutex.new

urls.each do |url|
  th << Thread.new(url) do |url|
    d = scraper.scrape url
    dlock.synchronize { data << d }
  end
end

th.each { |t| t.join }

TA-dah! (A warning written from memory, not verified, may eat your kitten, etc.)

Edit: I decided that someone should have written a generalized version of this, and so they have: http://peach.rubyforge.org/ - enjoy!

+5
source

Source: https://habr.com/ru/post/1703397/


All Articles