ruby threads

V

Vlad Smith

Hi guys! Hope you`ll find some time to assist with any ideas

i have an array of links and a number of steps that needs to be done to
each of those links.

there are always the same amount of links in array..so basically i can
do everything manually

i was wondering how can i create the new thread for processing each of
those links , maybe there`s a way of doing something like this :

array.each do |link|
a = Thread.new {....}
a.join
end
 
J

Justin Collins

Vlad said:
Hi guys! Hope you`ll find some time to assist with any ideas

i have an array of links and a number of steps that needs to be done to
each of those links.

there are always the same amount of links in array..so basically i can
do everything manually

i was wondering how can i create the new thread for processing each of
those links , maybe there`s a way of doing something like this :

array.each do |link|
a = Thread.new {....}
a.join
end

That almost works, except it will block on each thread before starting
the next.

threads = []

array.each do |link|
threads << Thread.new {....}
end

threads.each do |t|
t.join
end


-Justin
 
A

Aaron Patterson

Hi guys! Hope you`ll find some time to assist with any ideas

i have an array of links and a number of steps that needs to be done to
each of those links.

there are always the same amount of links in array..so basically i can
do everything manually

i was wondering how can i create the new thread for processing each of
those links , maybe there`s a way of doing something like this :

array.each do |link|
a = Thread.new {....}
a.join
end

Your code won't run anything in parallel. Thread#join will wait until
the thread is finished.

Try mapping your array to a list of threads, then iterating over that
list calling join:

array.map { |link|
Thread.new(link) { |l| ... }
}.each { |thread| thread.join }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top