Don't wait for Exception in threads

E

Emil Sandin

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
 
J

James Gray

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
 
A

akl

Hi Emil,

Emil said:
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
 
E

Emil Sandin

James said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top