S
Steve Tuckner
This is a multi-part message in MIME format.
------=_NextPart_000_00DF_01C35DA8.55D95060
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Recently I was helping a colleague who was new to Ruby with a program
that was using threads. It seemed to him that for some reason that some
of the thread's were not running. First he tried adding sleeps to the
main thread to "give time" to the other threads to run, but this didn't
work. It turns out that by joining the threads in question, that there
were errors in the program that were throwing exceptions that killed the
thread -- SILENTLY.
I don't know if there is a better way to do this, but this is the
solution we came up with to print out any exceptions that occur in a
thread (uncaught) and exit the program.
require "thread"
class MyThread < Thread
def initialize(*args, &block)
super(*args) do
begin
block.call
rescue Exception => e
$stderr.print "Exception occured: #{e.message} at " +
e.backtrace.join("\n")
exit! 1
end
end
end
end
Is there a better way?
Steve Tuckner
------=_NextPart_000_00DF_01C35DA8.55D95060
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D009342117-08082003>Recently I was=20
helping a colleague who was new to Ruby with a program that was using =
threads.=20
It seemed to him that for some reason that some of the thread's =
were not=20
running. First he tried adding sleeps to the main thread to "give time" =
to the=20
other threads to run, but this didn't work. It turns out that by =
joining the=20
threads in question, that there were errors in the program that were =
throwing=20
exceptions that killed the thread -- SILENTLY.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>I =
don't know if=20
there is a better way to do this, but this is the solution we came up =
with to=20
print out any exceptions that occur in a thread (uncaught) and exit the =
program.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D009342117-08082003>require=20
"thread"</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>class =
MyThread <=20
Thread<BR> def initialize(*args, =
&block)<BR> super(*args)=20
do<BR> begin<BR> block.call<BR>&=
nbsp; rescue=20
Exception =3D> e<BR> $stderr.print "Exception =
occured:=20
#{e.message} at " + =
e.backtrace.join("\n")<BR> exit!=20
1<BR> end<BR> end<BR> end<BR>end</SPAN>=
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Is =
there a better=20
way?</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Steve =
Tuckner</SPAN></FONT></DIV></BODY></HTML>
------=_NextPart_000_00DF_01C35DA8.55D95060--
------=_NextPart_000_00DF_01C35DA8.55D95060
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Recently I was helping a colleague who was new to Ruby with a program
that was using threads. It seemed to him that for some reason that some
of the thread's were not running. First he tried adding sleeps to the
main thread to "give time" to the other threads to run, but this didn't
work. It turns out that by joining the threads in question, that there
were errors in the program that were throwing exceptions that killed the
thread -- SILENTLY.
I don't know if there is a better way to do this, but this is the
solution we came up with to print out any exceptions that occur in a
thread (uncaught) and exit the program.
require "thread"
class MyThread < Thread
def initialize(*args, &block)
super(*args) do
begin
block.call
rescue Exception => e
$stderr.print "Exception occured: #{e.message} at " +
e.backtrace.join("\n")
exit! 1
end
end
end
end
Is there a better way?
Steve Tuckner
------=_NextPart_000_00DF_01C35DA8.55D95060
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D009342117-08082003>Recently I was=20
helping a colleague who was new to Ruby with a program that was using =
threads.=20
It seemed to him that for some reason that some of the thread's =
were not=20
running. First he tried adding sleeps to the main thread to "give time" =
to the=20
other threads to run, but this didn't work. It turns out that by =
joining the=20
threads in question, that there were errors in the program that were =
throwing=20
exceptions that killed the thread -- SILENTLY.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>I =
don't know if=20
there is a better way to do this, but this is the solution we came up =
with to=20
print out any exceptions that occur in a thread (uncaught) and exit the =
program.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D009342117-08082003>require=20
"thread"</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>class =
MyThread <=20
Thread<BR> def initialize(*args, =
&block)<BR> super(*args)=20
do<BR> begin<BR> block.call<BR>&=
nbsp; rescue=20
Exception =3D> e<BR> $stderr.print "Exception =
occured:=20
#{e.message} at " + =
e.backtrace.join("\n")<BR> exit!=20
1<BR> end<BR> end<BR> end<BR>end</SPAN>=
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Is =
there a better=20
way?</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D009342117-08082003></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Steve =
Tuckner</SPAN></FONT></DIV></BODY></HTML>
------=_NextPart_000_00DF_01C35DA8.55D95060--