On Wed, Nov 18, 2009 at 10:23 AM, pharrington <xenogene...@gmail.com> wrote:
> On Nov 18, 12:32 pm, Conrad Taylor <conra...@gmail.com> wrote: > > On Tue, Nov 17, 2009 at 1:07 PM, jhaagmans <jaap.haagm...@gmail.com> > wrote: > > > > > Hi, > > > > > I have a backgroundrb worker that gets triggered every second. When > > > it's triggered, it's supposed to make 2 - 15 http-requests using > > > Net::HTTP. My idea was to put every execution into a thread so the > > > next execution doesn't have to wait for the last one. So basically: > > > > > def http_requests > > > hosts.each do |host| > > > Thread.new do > > > begin > > > client = Net::HTTP.start(host) > > > rescue > > > #store host as inactive > > > ensure > > > client.finish if client.active? > > > end > > > end > > > end > > > end > > > > > Of course that's not all it does, but I hope you understand what I'm > > > trying to do here. > > > > > The thing is: this doesn't get done once a second. It appears that > > > every HTTP-request is waiting for the last one to complete, which > > > clots up Rails very fast! > > > > > My question is: why is this? Does this have anything to do with Ruby > > > not being threadsafe (I doubt it, because that just means threads > > > aren't executed as precisely as with jRuby, right?) or is Net::HTTP > > > not able to make requests while another Net::HTTP request is still > > > running? And what to do? > > > > > I hope you can help. > > > > The Global Interpreter Lock (GIL) prevents threads from executing in > > parallel when using Ruby 1.8.6 aka MRI, 1.8.7, and 1.9.1 aka YARV. > > However, JRuby 1.3.x/1.4.x, MacRuby 0.5 Beta 2, Maglev and several > > other upcoming Ruby VMs are not constrained by the GIL. Thus, they > > can execute threads in parallel. > > > > Good luck, > > > > -Conrad > > > > Emm just because the threads aren't all executing *simultaneously* > doesn't that they aren't running in parallel (due to all the thread > switching etc). > Each thread must acquire the lock before it can execute. Thus, it operates similar to a queue data structure (i.e. first in first out (FIFO)) and this is how it work today in regards to Ruby 1.8.6, 1.8.7, and 1.9.1. I know the C implementation of the Ruby VM very well. -Conrad > > Regardless, I can't seem to reproduce the OPs behviour: > > require 'net/http' > > def hosts > %w[rubyforge.org www.scala-lang.org www.google.com www.gamefaqs.com > allrecipes.com m2k2.taigaforum.com youtube.com gitorious.org > everything2.com] > end > > def http_requests > hosts.each do |host| > Thread.new do > begin > puts "fetching host #{host}" > client = Net::HTTP.start(host) > rescue e > #store host as inactive > ensure > puts "finished with host #{host}" > client.finish if client.active? > end > end > end > end > > irb(main):001:0> http_requests > fetching host rubyforge.orgfetching host www.scala-lang.orgfetching > host www.google.com > fetching host www.gamefaqs.com > fetching host allrecipes.com > fetching host m2k2.taigaforum.com > finished with host rubyforge.orgfetching host youtube.com > fetching host gitorious.org > finished with host www.google.com > fetching host everything2.com > => ["rubyforge.org", "www.scala-lang.org", "www.google.com", > "www.gamefaqs.com", "allrecipes.com", "m2k2.taigaforum.com", > "youtube.com", "gitorious.org", "everything2.com"] > irb(main):002:0> > > finished with host www.scala-lang.org > finished with host m2k2.taigaforum.comfinished with host www.gamefaqs.com > finished with host allrecipes.com > > > finished with host youtube.com > finished with host everything2.com > finished with host gitorious.org > > Am I missing something or misunderstanding the question? > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=. > > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.