From: Tony Arcieri Date: 2011-12-21T08:00:51+09:00 Subject: Re: ruby multithread --00032555f6eab9bd8004b48e0972 Content-Type: text/plain; charset=ISO-8859-1 This pattern should work, although it's a bit ugly: t1=Thread.new{work(url1)} t2=Thread.new{work(url2)} t3=Thread.new{work(url3)} t1.join t2.join t3.join A better approach would be: threads = [url1, url2, url3].map { |url| Thread.new { work(url) } } # joins each thread and obtains the value returned from the block you passed to Thread.new results = threads.map(&:value) however keep in mind that the standard Ruby interpreter has a Global Interpreter Lock (GIL), which means only one thread can run Ruby code at a given time (however certain things like IO can be performed outside the GIL) If you'd like to use Ruby without a GIL, take a look at JRuby or Rubinius 2.0 On Tue, Dec 20, 2011 at 1:31 PM, rubix Rubix wrote: > Hi, > > Thank you for your answer and sorry for the late, > I have a queue of url: > --------------------------------------- > Links =Queue.new > Links.push(url1) > Links.push(url2) > Links.push(url3) > > # a method to download page > def openurl(uri) > object_uri = open(url) > end > def ParseHtml(url) > Extract Meta > end > > def CommitPage(page) > insert meta to database (Mysql) > end > > def Work(url) > openurl(url) > ParseHtml(url) > CommitPage(page) > end > > > t1=Thread.new{work(url1)} > t2=Thread.new{work(url2)} > t3=Thread.new{work(url3)} > t1.join > t2.join > t3.join > ------------------------------------------ > The cpu is idle when executing methods openurl, commitpage because they > take time accessing data but I notice that when executing this script > t2 will wait for t1 until it finishes and t3 for t2 > > > have you a solution for this problem, because I am waisting a big time > like this > > best regards > > -- > Posted via http://www.ruby-forum.com/. > > -- Tony Arcieri --00032555f6eab9bd8004b48e0972--