# Thread safety in 2.2.2 - Thread.new { do\_sth } to do background job?

**URL:** <https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505>\
**Category:** Rails\
**Created:** [January 12, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505 "2009-01-12T12:55:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 12, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/1 "2009-01-12T12:55:12Z")

</div>

Hi guys,

I am writing application that is converting files from one format to  
another. The process takes some time for larger files, and I want to  
run it in backround. My application is running in threaded mode  
(enabled in environment.rb) and I know Rails 2.2.2 is thread safe.

Can I simply do in my controller:

def create  
@file = MyFile.new(params[:file])  
@file.save  
Thread.new {  
@file.convert  
}  
end

This seems to be working fine, but should I expect any problems or  
this is allowed in Rails 2.2.2, please?

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 13, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/2 "2009-01-13T23:53:51Z")

</div>

Pff,

what’s wrong with you guys – each time I need answer: nothing!

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 14, 2009, 12:02am UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/3 "2009-01-14T00:02:03Z")

</div>

Maybe no one has a real answer for you 🙂

From my experience, if you’re running ActiveRecord code, you could  
have a problem. I tried to use active record on non controller threads  
(even using with\_connection) and it didn’t work. If you’re not using  
ActiveRecord, it shoudn’t be an issue, just don’t forget that the MRI  
isn’t really concurrent and you’ll just get real concurrency running  
on JRuby.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

2009/1/13 Hubert £êpicki [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 14, 2009, 3:25am UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/4 "2009-01-14T03:25:11Z")

</div>

Hi Hubert,

If @file.convert is high cpu intensive task, you’d better use workers at  
background with a queue, or try jruby since MRI use green threads.

Jan

- Hubert ÅÄ™picki [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-01-12 03:54:34 -0800]:

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 14, 2009, 12:08am UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/5 "2009-01-14T00:08:30Z")

</div>

Thanks - well this may be issue. If it was easy to find solutions on  
google I would do it myself.

I am using ActiveRecord. The only problem I have noticed so far is  
that rails turn off class reloading when config.threadsafe is enabled

- and it makes my development pain (need to restart server each time  
after I change code).

On 14 Sty, 00:01, “Maurício Linhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 14, 2009, 6:25am UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/6 "2009-01-14T06:25:54Z")

</div>

- AD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-01-13 22:49:59 -0500]:

> i thought AR was thread-safe in 2.2 ?

It supposed to be. I have a small app use threads with sqlite3, and it  
runs well until now 🙂

Jan

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [January 14, 2009, 4:50am UTC](https://www.ruby-forum.com/t/thread-safety-in-2-2-2-thread-new-do-sth-to-do-background-job/157505/7 "2009-01-14T04:50:35Z")

</div>

i thought AR was thread-safe in 2.2 ?
