From: "ara.t.howard" Date: 2008-09-15T23:57:08+09:00 Subject: Re: How to control thread number On Sep 15, 2008, at 7:02 AM, Li Chen wrote: > Hi all, > > I copy the following script from Ruby programming language(bundling > with Ruby). If the size for pages (array size) is small,thread works > very well. If the array size is big (such 2000) the thread doesn't > work > at or very slow. I wonder what is the best means to control thread > number based on this script, read a small trunk of array at a time > using > each_slice method? > > Thanks, > > Li > > ##################### script##################### > 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 > -- > Posted via http://www.ruby-forum.com/. > this is exactly what threadify does cfp:~ > cat a.rb require 'open-uri' require 'rubygems' require 'threadify' # gem install threadify # # http://codeforpeople.com/lib/ruby/threadify/threadify-0.0.3/README uris = %w( http://www.ruby-lang.com http://www.awl.com http://www.pragmaticprogrammer.com ) uris.threadify(2) do |uri| tid = Thread.current.object_id response = open(uri){|fd| fd.read} puts "#{ tid } : #{ uri } => #{ response[0,42].inspect }" end cfp:~ > ruby a.rb 1867720 : http://www.ruby-lang.com => "\n\nRuby Land\n " "