views:

799

answers:

3

I was just reading up on ROR (haven't dived into it yet), and I hear that it isn't thread safe. Obviously, this doesn't mean that more than one person can't access your site at one time, so what exactly does it mean? Where do threads come into play in ROR? Do they just mean the request handling?

+4  A: 

Basically what it's saying is that you can't have multiple copies of rails running in the same process under different threads because it is possible for some of the resources to leak between the threads unintentionally causing weird behavior such as objects seemingly changing/disappearing at random times.

Additionally, it could also be the case that classes aren't designed with any synchronization built into them, making it hard to put parts of rails into threads and have other parts be shared among threads.

Stephen Caldwell
+24  A: 

Your information is out of date. It is thread safe as of 2.2.2

Keep in mind Ruby MRI 1.8.x, the most widely used implementation of Ruby uses Green Threads, so with 1.8.x if you create 100 threads they all run on the same CPU. Therefore when hosting Rails websites using MRI, you probably want as many instances of Ruby running as you have CPUS. Stuff like passenger takes care of this for you.

This used to be a big problem for JRuby, because JRuby has Native threads, and juggling processes seems superfluous. Anyway, its sorted out now.

On an aside, Iron Ruby, the .Net Ruby interpreter runs native threads.

Note: Ruby 1.9.1 uses native threads, but there is still a global interpreter lock in place.

Sam Saffron
+1  A: 

It's worth mentioning that Ruby MRI 1.8.x uses Green Threads, but Ruby MRI 2 will have native threads.

Benjamin Oakes
actually Ruby 1.9 already has native threads ..
Sam Saffron
What you say is true, but I drew the distinction because 1.9 is the experimental version leading up to 2.0. (Kinda like odd numbered Linux kernels are experimental and shouldn't be used in production.)
Benjamin Oakes
@Benjamin, 1.9 has been declared stable on the Ruby website, it is a massive change of architecture over 1.8 (since its based off yarv) I am not sure the scoping for features in 2.0 is even done.
Sam Saffron
Perhaps you could point me to where you got your information? What I see there is the following: Ruby 1.9.2 preview 1 released Ruby 1.9.2 preview 1 has been released. This is a preview for the 1.9.2 series. It is just a snapshot. It still have some known bugs, is sometimes unstable. Let us know your view on it.
Benjamin Oakes
For what it's worth, I've found out that 1.9.1 *is* stable. What I had written was based off an earlier announcement of the versioning strategy. (That is, what is now 1.9.1 was originally going to be called 2.0.)
Benjamin Oakes