Forum: Ruby on Rails Thread safety in 2.2.2 - Thread.new { do_sth } to do background job?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-12 12:55
(Received via mailing list)
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?
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-13 23:53
(Received via mailing list)
Pff,

what's wrong with you guys -- each time I need answer: nothing!
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-01-14 00:02
(Received via mailing list)
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/ (pt-br) | http://blog.codevader.com/
(en)



2009/1/13 Hubert £êpicki <hubert.lepicki@gmail.com>:
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-14 00:08
(Received via mailing list)
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" <mauricio.linha...@gmail.com>
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-01-14 03:25
(Received via mailing list)
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 <hubert.lepicki@gmail.com> [2009-01-12 03:54:34 -0800]:
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 AD (Guest)
on 2009-01-14 04:50
(Received via mailing list)
i thought AR was thread-safe in 2.2 ?
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-01-14 06:25
(Received via mailing list)
* AD <straightflush@gmail.com> [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
This topic is locked and can not be replied to.