[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

threads and errors

Charles Mills

12/20/2004 12:01:00 AM

What techniques are available for handling errors that occur in
threads?

For example how do I find out that an error occured in t below?

t = Thread.new { raise "error" }

I need to be able to check on the error status of threads from their
parent. Something like the following would be ideal - but I know this
won't do what I want:

begin
Thread.new { raise "error"}
rescue
puts $! # in a strange world prints "error", but in actuality does
nothing
end


-Charlie

13 Answers

nobu.nokada

12/20/2004 12:52:00 AM

0

Hi,

At Mon, 20 Dec 2004 09:02:12 +0900,
Charles Mills wrote in [ruby-talk:124052]:
> What techniques are available for handling errors that occur in
> threads?

Thread#join propagats the exception occurred in the thread to
the caller. Or, Thread#abort_on_exception flag directs to the
interpreter to abort.

> For example how do I find out that an error occured in t below?
>
> t = Thread.new { raise "error" }

begin
t.join
rescue
puts $!
end

--
Nobu Nakada


Joel VanderWerf

12/20/2004 6:45:00 AM

0

Charles Mills wrote:
> What techniques are available for handling errors that occur in
> threads?
>
> For example how do I find out that an error occured in t below?
>
> t = Thread.new { raise "error" }
>
> I need to be able to check on the error status of threads from their
> parent. Something like the following would be ideal - but I know this
> won't do what I want:
>
> begin
> Thread.new { raise "error"}
> rescue
> puts $! # in a strange world prints "error", but in actuality does
> nothing
> end

Another approach:

begin
Thread.new(Thread.current) do |th|
begin
raise "error"
rescue => e
th.raise e
end
end
rescue => e
p e
end


Robert Klemme

12/20/2004 9:22:00 AM

0


"Charles Mills" <cmills@freeshell.org> schrieb im Newsbeitrag
news:1103500878.714949.188970@c13g2000cwb.googlegroups.com...
> What techniques are available for handling errors that occur in
> threads?
>
> For example how do I find out that an error occured in t below?
>
> t = Thread.new { raise "error" }
>
> I need to be able to check on the error status of threads from their
> parent. Something like the following would be ideal - but I know this
> won't do what I want:
>
> begin
> Thread.new { raise "error"}
> rescue
> puts $! # in a strange world prints "error", but in actuality does
> nothing
> end

Apart from the other proposed solutions you can catch the exception at the
main level of the thread (Test 0) or you can use
"Thread.abort_on_exception = true" (Test 1 and 2):

10:20:08 [ruby]: ruby thread-abort-test.rb
#<RuntimeError: Test 0>
thread-abort-test.rb:2
thread-abort-test.rb:4
thread-abort-test.rb:6
thread-abort-test.rb:7: Test 2 (RuntimeError)
from thread-abort-test.rb:7:in `initialize'
from thread-abort-test.rb:7:in `new'
from thread-abort-test.rb:7
10:20:19 [ruby]: cat thread-abort-test.rb
Thread.new { begin; raise "Test 0"; rescue Exception => e; p e; end }
puts "#{__FILE__}:#{__LINE__}"
Thread.new { raise "Test 1" }
puts "#{__FILE__}:#{__LINE__}"
Thread.abort_on_exception = true
puts "#{__FILE__}:#{__LINE__}"
Thread.new { raise "Test 2" }
puts "#{__FILE__}:#{__LINE__}"

Kind regards

robert

nobu.nokada

12/20/2004 2:54:00 PM

0

Hi,

At Mon, 20 Dec 2004 15:45:10 +0900,
Joel VanderWerf wrote in [ruby-talk:124079]:
> Another approach:
>
> begin
> Thread.new(Thread.current) do |th|
> begin
> raise "error"
> rescue => e
> th.raise e
> end
> end
> rescue => e
> p e
> end

It's not predictable when the parent thread will be
interrupted. It may be after the rescue block.

--
Nobu Nakada


Charles Mills

12/20/2004 5:10:00 PM

0

Joel VanderWerf wrote:
> Charles Mills wrote:
> > What techniques are available for handling errors that occur in
> > threads?
> >
> > For example how do I find out that an error occured in t below?
> >
> > t = Thread.new { raise "error" }
> >
> > I need to be able to check on the error status of threads from
their
> > parent. Something like the following would be ideal - but I know
this
> > won't do what I want:
> >
> > begin
> > Thread.new { raise "error"}
> > rescue
> > puts $! # in a strange world prints "error", but in actuality does
> > nothing
> > end
>
> Another approach:
>
> begin
> Thread.new(Thread.current) do |th|
> begin
> raise "error"
> rescue => e
> th.raise e
> end
> end
> rescue => e
> p e
> end

Perfect, that is exactly what I was looking for. Basically my
situation is as follows:
####
..begin
.. Thread.new(Thread.current) do |th|
.. # do some processing
.. begin
.. raise "error"
.. rescue => e
.. th.raise e
.. end
.. end
.. # also do some processing
..rescue => e
.. puts e
..end
#### ignore leading dots - google groups seems to strip leading
whitespace :-/

Two threads asyncronously doing some processing, the child thread needs
to be able to notify the parent if errors occur during the processing
(rarely happens). The details are explained here:
http://rubyforge.org/pipermail/dnssd-developers/2004-December/0...
Thanks,
Charlie

Charles Mills

12/20/2004 5:19:00 PM

0

nobu.nokada@softhome.net wrote:
> Hi,
>
> At Mon, 20 Dec 2004 15:45:10 +0900,
> Joel VanderWerf wrote in [ruby-talk:124079]:
> > Another approach:
> >
> > begin
> > Thread.new(Thread.current) do |th|
> > begin
> > raise "error"
> > rescue => e
> > th.raise e
> > end
> > end
> > rescue => e
> > p e
> > end
>
> It's not predictable when the parent thread will be
> interrupted. It may be after the rescue block.

#### tmp.rb s/^\.//
..begin
.. Thread.new(Thread.current) do |th|
.. sleep 1
.. begin
.. raise "error"
.. rescue => e
.. th.raise e
.. end
.. end
..rescue => e
.. puts e
..end

..puts "before sleep"
..sleep 2
..puts "after sleep"
####
$ ruby tmp.rb
before sleep
tmp.rb:5: error (RuntimeError)
from tmp.rb:2:in `initialize'
from tmp.rb:2:in `new'
from tmp.rb:2
## If you comment out the 'sleep 1' line you get a much different
result: ##
$ ruby tmp.rb
error
before sleep
after sleep

Depending on the situation it may be necessary to keep the rest of the
code the begin/rescue block.

-Charlie

Al Nakba

2/17/2009 4:38:00 AM

0

On Feb 16, 3:12 pm, Froot Bat <m...@privacy.net> wrote:
> On Mon, 16 Feb 2009 11:11:18 -0800 (PST), Al Nakba
>
>
>
>
>
> <williamhubb...@bluebottle.com> wrote:
> >On Feb 16, 2:56 am, Mike Plowman <mike.plow...@mydomain.net> wrote:
> >> On Mon, 16 Feb 2009 10:40:54 +0000 (UTC), gb...@holyrood.ed.ac.uk (G
>
> >> Bell) wrote:
> >> >Al Nakba <williamhubb...@bluebottle.com> writes:
>
> >> >>fuck orff! Just don't watch top gear then..Asslifting muzzies have no
> >> >>sense of humour!
>
> >> >Hmm, this great English humourist was making jokes about wops on last
> >> >nights edition; as in "Wops" instead of "Cops" geddit...comedy genius I
> >> >don't think. I do see that he just spouts total crap thinking he is
> >> >being hilariously edgy which he's not.
>
> >> I always had the impression that he did it more to wind up the PC
> >> brigade.
>
> >Quite so. He relishes the opportunity..
>
> Yeah, I bet that's just what Hitler was doing too.- Hide quoted text -
>
> - Show quoted text -

Why? Did you ask him?

Al Nakba

2/18/2009 5:59:00 AM

0

On Feb 17, 4:41 pm, Froot Bat <m...@privacy.net> wrote:
> On Mon, 16 Feb 2009 20:37:30 -0800 (PST), Al Nakba
>
>
>
>
>
> <williamhubb...@bluebottle.com> wrote:
> >On Feb 16, 3:12 pm, Froot Bat <m...@privacy.net> wrote:
> >> On Mon, 16 Feb 2009 11:11:18 -0800 (PST), Al Nakba
>
> >> <williamhubb...@bluebottle.com> wrote:
> >> >On Feb 16, 2:56 am, Mike Plowman <mike.plow...@mydomain.net> wrote:
> >> >> On Mon, 16 Feb 2009 10:40:54 +0000 (UTC), gb...@holyrood.ed.ac.uk (G
>
> >> >> Bell) wrote:
> >> >> >Al Nakba <williamhubb...@bluebottle.com> writes:
>
> >> >> >>fuck orff! Just don't watch top gear then..Asslifting muzzies have no
> >> >> >>sense of humour!
>
> >> >> >Hmm, this great English humourist was making jokes about wops on last
> >> >> >nights edition; as in "Wops" instead of "Cops" geddit...comedy genius I
> >> >> >don't think. I do see that he just spouts total crap thinking he is
> >> >> >being hilariously edgy which he's not.
>
> >> >> I always had the impression that he did it more to wind up the PC
> >> >> brigade.
>
> >> >Quite so. He relishes the opportunity..
>
> >> Yeah, I bet that's just what Hitler was doing too.
>
> >Why? Did you ask him?
>
> Why? Did you ask Clarkson?- Hide quoted text -
>
> - Show quoted text -

Have a seance and ask Hitler..

Halmyre

2/18/2009 7:36:00 AM

0

On 16 Feb, 23:12, Froot Bat <m...@privacy.net> wrote:
> On Mon, 16 Feb 2009 11:11:18 -0800 (PST), Al Nakba
>
>
>
>
>
> <williamhubb...@bluebottle.com> wrote:
> >On Feb 16, 2:56 am, Mike Plowman <mike.plow...@mydomain.net> wrote:
> >> On Mon, 16 Feb 2009 10:40:54 +0000 (UTC), gb...@holyrood.ed.ac.uk (G
>
> >> Bell) wrote:
> >> >Al Nakba <williamhubb...@bluebottle.com> writes:
>
> >> >>fuck orff! Just don't watch top gear then..Asslifting muzzies have no
> >> >>sense of humour!
>
> >> >Hmm, this great English humourist was making jokes about wops on last
> >> >nights edition; as in "Wops" instead of "Cops" geddit...comedy genius I
> >> >don't think. I do see that he just spouts total crap thinking he is
> >> >being hilariously edgy which he's not.
>
> >> I always had the impression that he did it more to wind up the PC
> >> brigade.
>
> >Quite so. He relishes the opportunity..
>
> Yeah, I bet that's just what Hitler was doing too.- Hide quoted text -
>
> - Show quoted text -

So if Hitler had presented a popular motoring problem with Goering and
Goebbels and Herr Stig, WW2 might never have happened?

"Some say Herr Stig only has one ball...oh, wait a minute, that's
me..."

"Oh no, not a test of ze bloody Volkswagen again...aren't there any
other cars?"

--
Halmyre

Jeff Lawrence

2/18/2009 12:49:00 PM

0

On 18 Feb, 13:34, "John Rowland"
<jo...@journeyflow.spamspam.demon.co.uk> wrote:
> Al Nakba wrote:
>
> > Have a seance and ask Hitler..
>
> Why the seance? They have telephones in Argentina these days.

Yes, and apparently Adolf is still enjoying life on his little
Patagonian farm at the ripe old age of 119.
Cheers
Jeff