# Threading in ruby

**URL:** <https://www.ruby-forum.com/t/threading-in-ruby/139713>\
**Category:** Ruby\
**Created:** [June 6, 2008, 9:14am UTC](https://www.ruby-forum.com/t/threading-in-ruby/139713 "2008-06-06T09:14:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 6, 2008, 9:14am UTC](https://www.ruby-forum.com/t/threading-in-ruby/139713/1 "2008-06-06T09:14:59Z")

</div>

Hi  
I have succesfully send mail from my rails application…I did it like  
below.What I am doing is send a mail to an assignee.  
def after\_transfer  
code--------  
code------  
send\_mail\_to\_primary\_assignee(assignee\_id)  
end

#This is the function responsible for sending mails  
def send\_mail\_to\_primary\_assignee(contact\_id)  
contact\_name=Contact.find(contact\_id).name  
official\_email\_ids =  
ContactEmailAddress.find\_all\_by\_contact\_id(contact\_id ,:conditions =\>  
“contact\_email\_address\_type\_id = ‘2’” )  
official\_email\_ids.each do |official\_email|  
ServiceDeskMailer.deliver\_to\_send\_mail\_to\_primary\_assignee\_on\_transfer\_reason(official\_email,contact\_name)  
end  
puts "```````````````````` "  
end

Then my question is how can i put this to seperate thread in ruby…Why I  
am asking this is only after all the mails are send i can continue in  
the view…So if I put this to a thread is that problem solved .Since I  
am new thread especially in ruby could you please tell me how i can  
implement that?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 6, 2008, 11:10am UTC](https://www.ruby-forum.com/t/threading-in-ruby/139713/2 "2008-06-06T11:10:15Z")

</div>

2008/6/6 Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> def send\_mail\_to\_primary\_assignee(contact\_id)  
> Then my question is how can i put this to seperate thread in ruby…Why I  
> am asking this is only after all the mails are send i can continue in  
> the view…So if I put this to a thread is that problem solved .Since I  
> am new thread especially in ruby could you please tell me how i can  
> implement that?

I can see two reasonable options:

1. create a thread ad hoc and let it do the mail sending (i.e. call  
your methods).

2. create a global thread connected to a Queue that does the mail  
sending and stuff the email information into the queue if you want to  
send an email.

Note, both approaches are unlikely to work in a CGI environment. But  
since I do not have intimate knowledge of Rails I cannot really tell  
whether this will bite you or not. In this case you might need option

1. Create a separate process that accepts emails via DRb and then  
hands them off to your MTA. Do the threading internally like in 1 or
2. 

Cheers

robert
