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.