Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Don't wait for Exception in threads (http://www.velocityreviews.com/forums/t850084-dont-wait-for-exception-in-threads.html)

Emil Sandin 05-04-2008 06:48 PM

Don't wait for Exception in threads
 
I have two threads running in parallell, like this:

####################

t1=Thread.new{
50.times{
p "thread one"
sleep 0.1
}
}

t2=Thread.new{
p "thread two"
raise "error in thread two"
}

t1.join
t2.join

#################
The output is:
"thread one"
"thread two"
"thread one"
... 48 times more "thread one"
"thread one"
testscript.rb:15: error in thread two (RuntimeError)
from testscript.rb:19:in `join'
from testscript.rb:19

The program waits for the first thread to complete before telling me
that the second one raised an exception.
I don't want to wait until the first thread finishes to see the
exception, I want to see it right away.
How can I do that?

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


James Gray 05-04-2008 07:02 PM

Re: Don't wait for Exception in threads
 
On May 4, 2008, at 1:48 PM, Emil Sandin wrote:

> I have two threads running in parallell, like this:
>
> ####################
>
> t1=Thread.new{
> 50.times{
> p "thread one"
> sleep 0.1
> }
> }
>
> t2=Thread.new{
> p "thread two"
> raise "error in thread two"
> }
>
> t1.join
> t2.join
>
> #################
> The output is:
> "thread one"
> "thread two"
> "thread one"
> ... 48 times more "thread one"
> "thread one"
> testscript.rb:15: error in thread two (RuntimeError)
> from testscript.rb:19:in `join'
> from testscript.rb:19
>
> The program waits for the first thread to complete before telling me
> that the second one raised an exception.
> I don't want to wait until the first thread finishes to see the
> exception, I want to see it right away.
> How can I do that?


Add this line early in your program:

Thread.abort_on_exception = true

Hope that helps.

James Edward Gray II


akl 05-04-2008 07:03 PM

Re: Don't wait for Exception in threads
 
Hi Emil,

Emil Sandin schrieb:
> I have two threads running in parallell, like this:
>
> ####################
>
> t1=Thread.new{
> 50.times{
> p "thread one"
> sleep 0.1
> }
> }
>
> t2=Thread.new{
> p "thread two"
> raise "error in thread two"
> }
>
> t1.join
> t2.join
>
> #################
> The output is:
> "thread one"
> "thread two"
> "thread one"
> ... 48 times more "thread one"
> "thread one"
> testscript.rb:15: error in thread two (RuntimeError)
> from testscript.rb:19:in `join'
> from testscript.rb:19
>
> The program waits for the first thread to complete before telling me
> that the second one raised an exception.
> I don't want to wait until the first thread finishes to see the
> exception, I want to see it right away.
> How can I do that?


You can do something like:

Thread.abort_on_exception = true
t = []
t << Thread.new {
50.times{
p "thread one"
sleep 0.1
}
}
t << Thread.new {
p "thread two"
raise "error in thread two"
}
t.each {|t| t.join }

I dont have tested the example, but this way you should see the Exception in the
Moment it occurs

Andi

Emil Sandin 05-04-2008 07:07 PM

Re: Don't wait for Exception in threads
 
James Gray wrote:
> On May 4, 2008, at 1:48 PM, Emil Sandin wrote:
>
>>
>> "thread one"
>> I don't want to wait until the first thread finishes to see the
>> exception, I want to see it right away.
>> How can I do that?

>
> Add this line early in your program:
>
> Thread.abort_on_exception = true
>
> Hope that helps.
>
> James Edward Gray II


Thank you, that's exactly what I am looking for.

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



All times are GMT. The time now is 12:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.