Threads or multiple processes?

Is it more efficient to run multiple instances of a ruby ​​script? or multiple threads inside this single script?

take, for example, a ruby ​​script to check all links in a given domain.

Could you run multiple instances of this script or multiple threads?

how do you create multiple threads?

+3
source share
1 answer

"Efficiency" can mean many things. For Ruby <1.9, green threads mean you won’t get as much concurrency as you can think of from threads, so using multiple script instances would be the best way to minimize the total real time from start to finish.

, t Pickaxe "":

require 'net/http'


pages = %w( www.rubycentral.com
            www.awl.com
            www.pragmaticprogrammer.com
           )


threads = []


for page in pages
  threads << Thread.new(page) { |myPage|


    h = Net::HTTP.new(myPage, 80)
    puts "Fetching: #{myPage}"
    resp, data = h.get('/', nil )
    puts "Got #{myPage}:  #{resp.message}"
  }
end


threads.each { |aThread|  aThread.join }
+3

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


All Articles