Multithreading problem

Discussion in 'Ruby' started by Pen Ttt, Aug 12, 2010.

  1. Pen Ttt

    Pen Ttt Guest

    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/.
     
    Pen Ttt, Aug 12, 2010
    #1
    1. Advertising

  2. 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/.
     
    Paul Harrington, Aug 12, 2010
    #2
    1. Advertising

  3. Pen Ttt

    jason joo Guest

    Re: Multithreading problem

    [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 <>

    > 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/.
    >
    >
     
    jason joo, Aug 12, 2010
    #3
  4. Re: Multithreading problem

    On Thu, Aug 12, 2010 at 5:09 AM, Paul Harrington <> 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.
     
    Jesús Gabriel y Galán, Aug 12, 2010
    #4
  5. Brian Candler, Aug 12, 2010
    #5
  6. Re: Multithreading problem

    On Thu, Aug 12, 2010 at 9:53 AM, Brian Candler <> 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.
     
    Jesús Gabriel y Galán, Aug 12, 2010
    #6
  7. Re: Multithreading problem

    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/.
     
    Brian Candler, Aug 12, 2010
    #7
  8. Re: Multithreading problem

    On Thu, Aug 12, 2010 at 10:49 AM, Brian Candler <> 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
     
    Jesús Gabriel y Galán, Aug 12, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. insomniac

    ASP.Net 2.0 Multithreading Problem

    insomniac, Apr 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    2,730
    insomniac
    Apr 27, 2006
  2. Lee Garrington

    Multithreading beginner problem

    Lee Garrington, Dec 22, 2003, in forum: C++
    Replies:
    1
    Views:
    562
    Thomas Matthews
    Dec 22, 2003
  3. Replies:
    1
    Views:
    3,157
    bruce barker \(sqlwork.com\)
    May 11, 2006
  4. Diez B. Roggisch

    multithreading-problem

    Diez B. Roggisch, Jul 27, 2003, in forum: Python
    Replies:
    4
    Views:
    344
    Diez B. Roggisch
    Jul 27, 2003
  5. abhinav

    Python multithreading problem

    abhinav, Mar 26, 2006, in forum: Python
    Replies:
    3
    Views:
    383
    abhinav
    Mar 27, 2006
Loading...

Share This Page