Design question on threads.

S

Srijayanth Sridhar

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
 
J

Jano Svitok

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
<here comes your code>
rescue Exception
end
end
end
 
E

Erik Veenstra

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/
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top