# Multithreading problem

**URL:** <https://www.ruby-forum.com/t/multithreading-problem/193148>\
**Category:** Ruby\
**Created:** [August 12, 2010, 5:05am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148 "2010-08-12T05:05:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 5:05am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/1 "2010-08-12T05:05:01Z")

</div>

probem1:  
the following program can only run in irb console,it can’t run with  
command :  
ruby /home/test.rb,why?  
require ‘rubygems’  
require ‘net/http’  
threads = []  
open(“/home/pt/test/data”,“a+”) do |wfile|  
str=%w([http://table.finance.yahoo.com/table.csv?s=IBM](http://table.finance.yahoo.com/table.csv?s=IBM)  
[http://table.finance.yahoo.com/table.csv?s=YHOO](http://table.finance.yahoo.com/table.csv?s=YHOO)  
[http://table.finance.yahoo.com/table.csv?s=AACC](http://table.finance.yahoo.com/table.csv?s=AACC))  
for page\_to\_fetch in str  
Thread.new(page\_to\_fetch) do |url|  
info = Net::HTTP.get\_response(URI.parse(url)).body  
puts info  
end  
end  
threads.each {|thr| thr.join}  
end  
problem2:  
i want to write output into my file,just change “puts info” into  
“wfile.puts info”,it can not run.  
require ‘rubygems’  
require ‘net/http’  
threads = []  
open(“/home/pt/test/data”,“a+”) do |wfile|  
str=%w([http://table.finance.yahoo.com/table.csv?s=IBM](http://table.finance.yahoo.com/table.csv?s=IBM)  
[http://table.finance.yahoo.com/table.csv?s=YHOO](http://table.finance.yahoo.com/table.csv?s=YHOO)  
[http://table.finance.yahoo.com/table.csv?s=AACC](http://table.finance.yahoo.com/table.csv?s=AACC))  
for page\_to\_fetch in str  
Thread.new(page\_to\_fetch) do |url|  
info = Net::HTTP.get\_response(URI.parse(url)).body  
wfile.puts info  
end  
end  
threads.each {|thr| thr.join}  
end

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 5:09am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/2 "2010-08-12T05:09:53Z")

</div>

Pen T. wrote:

> probem1:  
> the following program can only run in irb console,it can’t run with  
> command :  
> ruby /home/test.rb,why?  
> require ‘rubygems’  
> require ‘net/http’  
> threads = []  
> open(“/home/pt/test/data”,“a+”) do |wfile|  
> str=%w([http://table.finance.yahoo.com/table.csv?s=IBM](http://table.finance.yahoo.com/table.csv?s=IBM)  
> [http://table.finance.yahoo.com/table.csv?s=YHOO](http://table.finance.yahoo.com/table.csv?s=YHOO)  
> [http://table.finance.yahoo.com/table.csv?s=AACC](http://table.finance.yahoo.com/table.csv?s=AACC))  
> for page\_to\_fetch in str  
> Thread.new(page\_to\_fetch) do |url|  
> info = Net::HTTP.get\_response(URI.parse(url)).body  
> puts info  
> end  
> end  
> threads.each {|thr| thr.join}  
> end

threads is still an empty array. Try adding each thread to threads as  
you create it.

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 6:41am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/3 "2010-08-12T06:41:28Z")

</div>

u forgot to add it to ur thread list array “threads[]” after u created a  
thread.  
so the array “threads[]” left empty and u joined nothing in the end  
then these threads were terminated because the main thread was  
terminated  
and u got nothing in that file

2010/8/12 Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 9:53am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/4 "2010-08-12T09:53:01Z")

</div>

Or simply:

threads \<\< Thread.new do  
…  
end

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 9:27am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/5 "2010-08-12T09:27:07Z")

</div>

On Thu, Aug 12, 2010 at 5:09 AM, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > [http://table.finance.yahoo.com/table.csv?s=YHOO](http://table.finance.yahoo.com/table.csv?s=YHOO)  
> > threads is still an empty array. Try adding each thread to threads as  
> > you create it.

An idiom I usually use is this:

threads = str.map do  
Thread.new […snip…]  
end

threads.each {|t| t.join}

Jesus.

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 10:09am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/6 "2010-08-12T10:09:16Z")

</div>

On Thu, Aug 12, 2010 at 9:53 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Or simply:
> 
> threads \<\< Thread.new do  
> …  
> end

But you still need the outer iteration. If I’m already iterating with  
#each, I change it to map to do both things at the same time 🙂

Jesus.

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 10:49am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/7 "2010-08-12T10:49:20Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> But you still need the outer iteration. If I’m already iterating with  
> #each, I change it to map to do both things at the same time 🙂

Of course. But for a newcomer to ruby (who is still using ‘for’ instead  
of ‘each’), it may be easier to take one step at a time along the road  
to enlightenment.

Step 1:

threads = []  
for page in str  
threads \<\< Thread.new do  
…  
end  
end

Step 2:

threads = []  
str.each do |page|  
threads \<\< Thread.new do  
…  
end  
end

Step 3:

threads = str.map do |page|  
Thread.new do  
…  
end  
end

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [August 12, 2010, 11:37am UTC](https://www.ruby-forum.com/t/multithreading-problem/193148/8 "2010-08-12T11:37:40Z")

</div>

On Thu, Aug 12, 2010 at 10:49 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Jesús Gabriel y Galán wrote:
> 
> > But you still need the outer iteration. If I’m already iterating with  
> > #each, I change it to map to do both things at the same time 🙂
> 
> Of course. But for a newcomer to ruby (who is still using ‘for’ instead  
> of ‘each’), it may be easier to take one step at a time along the road  
> to enlightenment.

Yes, you are right.
