Multithreading problem

Discussion in 'Ruby' started by Venkat Bagam, Feb 27, 2008.

  1. Venkat Bagam

    Venkat Bagam Guest

    Hi All,

    I am pretty much a starter in multi-threading. In one of my
    application, I needed to run three threads along each other. Here is my
    code

    require 'rubygems'
    require 'mechanize'
    require 'fileutils'
    require 'timeout'

    agent = WWW::Mechanize.new

    start_time = Time.now
    threads = []

    threads << Thread.new do
    puts "running thread1"
    start = 0
    count = 10
    finish = 30
    while start < finish
    page_links = Array.new
    parent_url = "http://www.rknowsys.com"
    page = agent.get(parent_url)
    page = nil
    start += count
    end
    puts "thread1 completed" if start == finish
    end

    threads << Thread.new do
    puts "running thread2"
    start = 30
    count = 10
    finish = 60
    while start < finish
    page_links = Array.new
    parent_url = "http://www.rknowsys.com"
    page = agent.get(parent_url)
    page = nil
    start += count
    raise "stopped bcaz start > 40" if start > 40
    end
    puts "thread2 completed" if start == finish
    end

    threads.each do |thread|
    begin
    thread.join
    rescue RuntimeError => e
    puts e.message
    end
    end

    end_time = Time.now
    interval = end_time - start_time
    puts interval

    the above code prints the error message, at the time of each thread join
    and stops.
    But, How do I deal with such a scenario that the thread2 continues to
    run again setting its local variable start = 30 ?

    Any help appreciated.

    regards,
    Venkat Bagam
    --
    Posted via http://www.ruby-forum.com/.
     
    Venkat Bagam, Feb 27, 2008
    #1
    1. Advertising

  2. Venkat Bagam

    7stud -- Guest

    Venkat Bagam wrote:
    > the above code prints the error message, at the time of each thread join
    > and stops.
    > But, How do I deal with such a scenario that the thread2 continues to
    > run again


    How would thread2 be made to run again?
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 27, 2008
    #2
    1. Advertising

  3. Venkat Bagam wrote:
    > Hi All,
    >
    > I am pretty much a starter in multi-threading. In one of my
    > application, I needed to run three threads along each other. Here is my
    > code
    >
    > require 'rubygems'
    > require 'mechanize'
    > require 'fileutils'
    > require 'timeout'
    >
    > agent = WWW::Mechanize.new
    >
    > start_time = Time.now
    > threads = []
    >
    > threads << Thread.new do
    > puts "running thread1"
    > start = 0
    > count = 10
    > finish = 30
    > while start < finish
    > page_links = Array.new
    > parent_url = "http://www.rknowsys.com"
    > page = agent.get(parent_url)
    > page = nil
    > start += count
    > end
    > puts "thread1 completed" if start == finish
    > end
    >
    > threads << Thread.new do
    > puts "running thread2"
    > start = 30
    > count = 10
    > finish = 60
    > while start < finish
    > page_links = Array.new
    > parent_url = "http://www.rknowsys.com"
    > page = agent.get(parent_url)
    > page = nil
    > start += count
    > raise "stopped bcaz start > 40" if start > 40
    > end
    > puts "thread2 completed" if start == finish
    > end
    >
    > threads.each do |thread|
    > begin
    > thread.join
    > rescue RuntimeError => e
    > puts e.message
    > end
    > end
    >
    > end_time = Time.now
    > interval = end_time - start_time
    > puts interval
    >
    > the above code prints the error message, at the time of each thread join
    > and stops.
    > But, How do I deal with such a scenario that the thread2 continues to
    > run again setting its local variable start = 30 ?
    >
    > Any help appreciated.
    >
    > regards,
    > Venkat Bagam
    >


    What is the error message?
     
    Justin Collins, Feb 27, 2008
    #3
  4. Venkat Bagam

    7stud -- Guest

    Justin Collins wrote:
    > Venkat Bagam wrote:
    >>
    >> while start < finish
    >> puts "running thread2"
    >> end
    >>
    >>
    >> regards,
    >> Venkat Bagam
    >>

    >
    > What is the error message?


    raise "stopped bcaz start > 40" if start > 40
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 27, 2008
    #4
  5. Venkat Bagam

    Venkat Bagam Guest

    threads.each do |thread|
    begin
    thread.join
    rescue RuntimeError => e
    puts e.message
    end
    end

    right now i was displaying a customized error message "stopped bcaz
    start > 40" in rescue. But if I want to re run the thread rather than
    printing the message, is it possible to do? I tried "thread.run",
    "thread.wakeup" etc. but it shits something like "undefined method run
    called for dead thread" etc.....

    any guess?

    regards,
    Venkat Bagam
    --
    Posted via http://www.ruby-forum.com/.
     
    Venkat Bagam, Feb 28, 2008
    #5
  6. Venkat Bagam

    7stud -- Guest

    Venkat Bagam wrote:
    > threads.each do |thread|
    > begin
    > thread.join
    > rescue RuntimeError => e
    > puts e.message
    > end
    > end
    >



    >But if I want to re run the thread


    Threads are created, they run, and then they cease to exist. You can't
    rerun a thread.
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 28, 2008
    #6
  7. Venkat Bagam

    7stud -- Guest

    7stud -- wrote:
    >
    > Threads are created, they run, and then they cease to exist. You can't
    > rerun a thread.
    >


    However, you can run a new thread and have it use some values that a
    previous thread inserted into a global variable.
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 28, 2008
    #7
    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,764
    insomniac
    Apr 27, 2006
  2. Lee Garrington

    Multithreading beginner problem

    Lee Garrington, Dec 22, 2003, in forum: C++
    Replies:
    1
    Views:
    568
    Thomas Matthews
    Dec 22, 2003
  3. Replies:
    1
    Views:
    3,182
    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:
    352
    Diez B. Roggisch
    Jul 27, 2003
  5. abhinav

    Python multithreading problem

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

Share This Page