Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Multithreading problem

Reply
Thread Tools

Multithreading problem

 
 
Pen Ttt
Guest
Posts: n/a
 
      08-12-2010
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=YHOO
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=YHOO
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Paul Harrington
Guest
Posts: n/a
 
      08-12-2010
Pen Ttt 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=YHOO
> 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
jason joo
Guest
Posts: n/a
 
      08-12-2010
[Note: parts of this message were removed to make it a legal post.]

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 Ttt <(E-Mail Removed)>

> 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=YHOO
> 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=YHOO
> 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
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-12-2010
On Thu, Aug 12, 2010 at 5:09 AM, Paul Harrington <(E-Mail Removed)> wrot=
e:
> Pen Ttt wrote:
>> probem1:
>> the following program can only run =A0in irb console,it can't run with
>> command :
>> ruby /home/test.rb,why?
>> require 'rubygems'
>> require 'net/http'
>> threads =3D []
>> open("/home/pt/test/data","a+") do |wfile|
>> str=3D%w(http://table.finance.yahoo.com/table.csv?s=3DIBM
>> http://table.finance.yahoo.com/table.csv?s=3DYHOO
>> http://table.finance.yahoo.com/table.csv?s=3DAACC)
>> for page_to_fetch in str
>> =A0 =A0Thread.new(page_to_fetch) do |url|
>> =A0 =A0 info =3D Net::HTTP.get_response(URI.parse(url)).body
>> =A0 =A0 puts info
>> =A0 =A0end
>> =A0end
>> threads.each {|thr| thr.join}
>> end

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


An idiom I usually use is this:

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

threads.each {|t| t.join}

Jesus.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      08-12-2010
Or simply:

threads << Thread.new do
...
end

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-12-2010
On Thu, Aug 12, 2010 at 9:53 AM, Brian Candler <(E-Mail Removed)> wrote:
> Or simply:
>
> =A0threads << Thread.new do
> =A0 =A0...
> =A0end


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.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      08-12-2010
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-12-2010
On Thu, Aug 12, 2010 at 10:49 AM, Brian Candler <(E-Mail Removed)> wrote=
:
> Jes=FAs Gabriel y Gal=E1n 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.


>
> Step 1:
>
> threads =3D []
> for page in str
> =A0threads << Thread.new do
> =A0 =A0...
> =A0end
> end
>
> Step 2:
>
> threads =3D []
> str.each do |page|
> =A0threads << Thread.new do
> =A0 =A0...
> =A0end
> end
>
> Step 3:
>
> threads =3D str.map do |page|
> =A0Thread.new do
> =A0 =A0...
> =A0end
> end


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with multithreading and Web services method invocation abhra.haldar@gmail.com ASP .Net 1 05-11-2006 04:21 PM
ASP.Net 2.0 Multithreading Problem insomniac ASP .Net 2 04-27-2006 05:03 PM
Python multithreading problem abhinav Python 3 03-27-2006 11:03 AM
Multithreading beginner problem Lee Garrington C++ 1 12-22-2003 02:12 PM
multithreading-problem Diez B. Roggisch Python 4 07-27-2003 08:35 PM



Advertisments