[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multithreading problem

Venkat Bagam

2/27/2008 8:16:00 AM

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

12 Answers

7stud --

2/27/2008 9:47:00 AM

0

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

Justin Collins

2/27/2008 7:02:00 PM

0

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...
> 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...
> 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?



7stud --

2/27/2008 8:49:00 PM

0

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

Venkat Bagam

2/28/2008 1:46:00 PM

0

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

7stud --

2/28/2008 5:32:00 PM

0

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

7stud --

2/28/2008 5:35:00 PM

0

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

Paul Duca

1/5/2013 12:41:00 AM

0

On Friday, January 4, 2013 6:45:43 PM UTC-5, Tom Gardner wrote:
> On 1/4/2013 5:23 PM, Scout wrote:
>
> >
>
> >
>
> > "Tom Gardner" <Mars@Tacks> wrote in message
>
> > news:Ntydnbg70uocgHrNnZ2dnUVZ_jydnZ2d@giganews.com...
>
> >> On 1/3/2013 8:29 PM, Scout wrote:
>
> >>>
>
> >>> With a shelf life in excess of 100 years and the fact that criminals
>
> >>> need/use VERY little ammunition.
>
> >>>
>
> >>
>
> >> But criminals train vigorously! I'm sure they each go through 1k
>
> >> rounds per month like I do.
>
> >
>
> > So you're a criminal?
>
> >
>
> >
>
>
>
> HEY! I could be a GREAT criminal, if I could just learn to shed my
>
> morals and learn to be...a ...leftist!

Likely you'd make more money than whatever menial you have now...


Paul

Morgoth Bauglir

1/5/2013 9:00:00 PM

0


> On 01/05/2013 01:27 PM, Morgoth Bauglir wrote:
>>
>>
>>> Will it be bloody and nasty? Hell yes. Will the Government win?
>>> Not a fucking chance.
>>>
>>> Gunner
>>>
>> You provided the perfect example, for which I thank you.
>>
>> Waco.
>>
>> I'm sure David Koresh talked just like you.
>
> I am sure that he did not.
>
> Waco was one small group. That group had no support from the people, and
> no means of resupply. Suppose they had had the support of the local
> community, and suppose they had developed a logistics system which would
> allow them to resupply.

You'd call that a government.

> Now suppose that you have several thousand of
> these "Wacos" going on simultaneously all around the nation.

I wouldn't hold my breath waiting for that to happen.
>
> I am not sure about other states, but in our state, any just about any
> town of any size has a Guard unit(either Air or Army). Do you really
> think that those guardsmen are going to obey orders to turn on their
> fellow citizens?


They did in Arkansas in 1957.
In fact, when Eisenhower federalized the Arkansas National Guard, they went
without a squawk.
In defiance of the Governor of Arkansas.

Tom Gardner

1/6/2013 1:19:00 AM

0

On 1/5/2013 3:06 PM, wy wrote:

> You're delusional, as usual. People don't stand a chance. The
> government's got all the fire power. All they have to do is send in
> the drones and you're toast...


And two camel jockeys with an old cell phone can hack Obammy's drones.

Tom Gardner

1/6/2013 1:25:00 AM

0

On 1/5/2013 3:08 PM, Gunner wrote:
> On Sat, 05 Jan 2013 13:03:55 -0500, Tom Gardner <Mars@Tacks> wrote:
>
>> On 1/5/2013 12:05 PM, Gunner wrote:
>>> On Sat, 05 Jan 2013 09:51:54 -0500, Tom Gardner <Mars@Tacks> wrote:
>>>
>>>> On 1/4/2013 2:11 PM, David Hartung wrote:
>>>>> On 01/04/2013 12:25 PM, FirstPost wrote:
>>>>>> On Fri, 04 Jan 2013 13:20:27 -0500, Tom Gardner <Mars@Tacks> wrote:
>>>>>
>>>>>>> But criminals train vigorously! I'm sure they each go through 1k rounds
>>>>>>> per month like I do.
>>>>>>
>>>>>> Yes indeed. That's why most can't hit what they aim at except at
>>>>>> extreme close range because they are so fond of holding the gun
>>>>>> sideways like their heroes. the gangstas in their favorite
>>>>>> blacksploitation movies.
>>>>>> A life and death situation and they do something that stupid because
>>>>>> "it looks cool" to them.
>>>>>
>>>>> Had a man tell my yesterday, that even though he hasn't fored a weapon
>>>>> in probably 25 years, he is still competent to shoot accurately. This
>>>>> guy also has vision issues.
>>>>>
>>>>> Competent, mature firearms use requires regular practice.
>>>>>
>>>>
>>>> If I miss a week or so of practice it's VERY noticeable! The pistol is
>>>> heavy, the sight orbit is huge and trigger pull is jerky.
>>>
>>> You havent got it ingrained into muscle memory yet.
>>>
>>> Keep working on it. Its very Zen.
>>>
>>> Gunner, who won a combat match after not having picked up a gun in
>>> nearly 4 yrs
>>> The methodology of the left has always been:
>>>
>>> 1. Lie
>>> 2. Repeat the lie as many times as possible
>>> 3. Have as many people repeat the lie as often as possible
>>> 4. Eventually, the uninformed believe the lie
>>> 5. The lie will then be made into some form oflaw
>>> 6. Then everyone must conform to the lie
>>>
>>
>> I know. I have a 19 that always cooperates but others require thought.
>
> Then use the 19 as your "duty" weapon. Ive always liked em and own
> several. They simply "point" properly. The L frame..586 is almost as
> natural.
>
> The only reason I can shoot the 1911 as well as I can..is muscle
> memory. After the first 100,000 rds..it started becoming pretty
> natural....<G>
>
> Gunner
>
> The methodology of the left has always been:
>
> 1. Lie
> 2. Repeat the lie as many times as possible
> 3. Have as many people repeat the lie as often as possible
> 4. Eventually, the uninformed believe the lie
> 5. The lie will then be made into some form oflaw
> 6. Then everyone must conform to the lie
>

My 19-eleven is second on the list only because I actually like to load
those soup cans so they get used more. They go through the progressive
machine without a glitch. In contrast I hate 9's, it's the damn taper
and small size.