Reputation: 2363
I'm trying to use multithreading in ruby for heaving a lot of Network-Connections at the same time but I really stuck at the basics.
I tried this:
for i in 1..1000 do
Thread.new{load(i)}
end
def load(i)
File.open(filePath, "w") do |output|
open(imageURL) do | input |
output << input.read
end
end
end
This is only a part of the Download-Script for showing what I'm doing. At real I first load some HTML, parse it with Nokogiri and so on...
I'm running this script in Terminal with "ruby script.rb" - and nothing - really nothing happened.
Any idea how to solve this?
Thanks allot Chris
Upvotes: 4
Views: 1998
Reputation: 39650
Your script will immediately exit, because you are not waiting for your threads to finish.
Consider the first example of this section of the Pickaxe - you need to join your threads, in order to actually wait for all of them to finish their jobs.
So you should rather try this:
def load(i)
...
end
threads = []
for i in 1..1000 do
threads << Thread.new { load(i) }
end
threads.each { |t| t.join }
Upvotes: 2