[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Design question on threads.

Srijayanth Sridhar

6/12/2008 7:45:00 AM

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

2 Answers

Jano Svitok

6/12/2008 8:50:00 AM

0

On Thu, Jun 12, 2008 at 09:44, Srijayanth Sridhar <srijayanth@gmail.com> 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
<here comes your code>
rescue Exception
end
end
end

Erik Veenstra

6/12/2008 10:05:00 AM

0

> 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.erikve...