ruby-on-railsgreen-threads

Using Thread.new to send email on rails


I've been sending emails on my application (ruby 1.8.7, rails 2.3.2) like this Thread.new{UserMailer.deliver_signup_notification(user)}

Since ruby use green threads, there's any performance advantage doing this, or I can just use UserMailer.deliver_signup_notification(user) ?

Thanks


Solution

  • Global VM lock will still almost certainly apply while sending that email, meaning no difference.

    You should not start threads in a request/response cycle. You should not start threads at all unless you can watch them from create to join, and even then, it is rarely worth the trouble it creates.

    Rails is not thread-safe, and is not meant to be from within your controller actions. Only since Rails 2.3 has just dispatching been thread-safe, and only if you turn it on in environment.rb with config.threadsafe!.

    This article explains in more detail. If you want to send your message asynchronously use BackgroundRb or its analog.