Design question on threads

Hello,

I have a script that launches a bunch of threads. Each thread sleeps
for the most part, wakes up, fetches a page, does some parsing, and
goes back to sleep again. Really simple. However, on occasion, either
the parser or the httpclient raises an exception and the thread dies.
I can set Thread.abort_on_exception to true. But I’d much rather that
the thread just restarts. More often than not these exceptions are not
cause enough for the thread to die.

Someone suggested to me on the ruby irc channel to wrap the thread in
a class that will restart itself if the thread died. Does anyone have
any decent example of this or any suggestion regarding the matter?

Thank you,

J

On Thu, Jun 12, 2008 at 09:44, Srijayanth S. [email protected]
wrote:

the parser or the httpclient raises an exception and the thread dies.
I can set Thread.abort_on_exception to true. But I’d much rather that
the thread just restarts. More often than not these exceptions are not
cause enough for the thread to die.

put your thread body in a while loop and catch the exceptions inside
the loop. don’t forget to provide a
means to stop it

def run
while true
begin

rescue Exception
end
end
end

I have a script that launches a bunch of threads. Each thread
sleeps for the most part, wakes up, fetches a page, does some
parsing, and goes back to sleep again. Really simple.
However, on occasion, either the parser or the httpclient
raises an exception and the thread dies. I can set
Thread.abort_on_exception to true. But I’d much rather that
the thread just restarts. More often than not these
exceptions are not cause enough for the thread to die.

I would rather keep the retry in the body of the thread. I
suppose you’ve something like this:

Thread.fork do
loop do
# The real stuff…

 sleep 60

end
end

Simply add the retry:

Thread.fork do
loop do
begin
# The real stuff…
rescue SomeException # Be careful!
sleep 1

   retry
 end

 sleep 60

end
end

gegroet,
Erik V. - http://www.erikveen.dds.nl/