# Design question on threads

**URL:** <https://www.ruby-forum.com/t/design-question-on-threads/140255>\
**Category:** Ruby\
**Created:** [June 12, 2008, 10:30am UTC](https://www.ruby-forum.com/t/design-question-on-threads/140255 "2008-06-12T10:30:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [June 12, 2008, 10:30am UTC](https://www.ruby-forum.com/t/design-question-on-threads/140255/1 "2008-06-12T10:30:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [June 12, 2008, 10:55am UTC](https://www.ruby-forum.com/t/design-question-on-threads/140255/2 "2008-06-12T10:55:31Z")

</div>

On Thu, Jun 12, 2008 at 09:44, Srijayanth S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [June 12, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/design-question-on-threads/140255/3 "2008-06-12T13:43:38Z")

</div>

> 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/](http://www.erikveen.dds.nl/)
