Is Ruby's Net::HTTP threadsafe?
(Aside from the version_1_1 and version_1_2 methods which it explicitly says aren't)
Unfortunately, Ruby doesn't ship with any thread-safe Array or Hash implementations. The core Array and Hash classes are not thread-safe by default, nor should they be.
Thread-safety in Rails the framework This meant that while a request was being processed, no other thread in the same process could be running.
Rails as a framework is thread-safe. So, the answer is yes! The second link that you posted here names many of the Rails servers that don't do well with multi-threading. He later mentions that nginx is the way to go (it is definitely the most popular one and highly recommended).
In particular, Ruby concurrency is when two tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean, though, that they'll ever both be running at the same instant (e.g., multiple threads on a single-core machine).
I wouldn't count on it.
In 2008, matz wrote:
For MRI (1.8.x) and YARV (1.9.x), every C implemented methods are protected by GIL (Global Interpreter Lock), so that you don't have to worry about. But it might depend on each implementation.
Net::HTTP is in stdlib, which means it is not implemented in C (or at least, not fully implemented in C). I am assuming that matz's note on the GIL is still correct today, which would imply that the GIL would not be placed on Net::HTTP. Therefore, I doubt it would be threadsafe.
I have unfortunately not found definitive evidence in the docs for the current version of Ruby, though I feel it is also worth mentioning this bit from Concurrency in jruby:
At least these classes [core classes and classes in the stdlib] are not considered thread-safe, and if you intend to mutate them concurrently with other operations you will want to introduce locking (e.g. with Mutex): String, Array, Hash, and any data structures derived from them.
I feel it would be best to play it safe by either adding locks around Net::HTTP or using a threadsafe alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With