Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what way is Ruby on Rails NOT multithreaded?

Tags:

Disclaimer: I'm a c# ASP.NET developer learning "RoR". Sorry if this question doesn't "get" RoR, any corrections greatly appreciated!

What is multithreading

My understanding of "multithread" ability in web apps is twofold:

  1. Every time a web/app server receives a request it can assign a thread to the new request, thus multiple requests can run concurrently.
  2. The app runtime + language allows for multiple threads to be used WITHIN a single request (in ASP.NET via "Async" methods and keywords for example).

In this way, IIS7 + ASP.NET can do points 1 AND 2.

I'm confused about RoR

I've read these two articles and they have left me confused:

question one.

I think I understand that RoR doesn't lend itself very well to point number 2 above, that is, having multiple threads within the same request, have I got that right?

question two.

Just to be crystal clear, RoR app/web servers can also do point number 1 above right (that is, multiple requests can run concurrently)? is that not always the case with RoR?

like image 210
andy Avatar asked Oct 07 '12 05:10

andy


People also ask

Is Ruby on Rails multithreaded?

Dissecting Ruby on Rails 5 - Become a Professional Developer Often on a single CPU machine, multiple threads are not actually executed in parallel, but parallelism is simulated by interleaving the execution of the threads. Ruby makes it easy to write multi-threaded programs with the Thread class.

Is Ruby multithreaded or single threaded?

The Ruby Interpreter is single threaded, which is to say that several of its methods are not thread safe. In the Rails world, this single-thread has mostly been pushed to the server.

Why is Rails single threaded?

It's not a common production platform among the RoR community. As a result, Eventhough Rails itself is thread-safe since version 2.2, there isn't yet a good multi-threaded server for it on Windows servers. And you get the best results by running it on *nix servers using multi-process/single-threaded concurrency model.

Can Ruby use multiple cores?

A single thread executes on one CPU core, so if you write a ruby program, then it is executed only one core of CPU and if you have quad-core CPU then other 3 cores are not utilize to execute your ruby program. Threading makes ruby program to utilize more memory and CPU to execute faster and achieve concurrency.


1 Answers

Question 1: You can spawn more Ruby threads in one request if you want, although that seems to be outside the typical use case for Rails. There are uses for it for certain long-running IO or external operations.

Question 2: The limiting factor for Ruby concurrency in general, not just with Rails, is the Global Interpreter Lock. This feature of Ruby prevents more than 1 thread of Ruby from executing at any given time per process. The lock is released whenever there is non-Ruby code executing, such as waiting for disk IO or SQL responses. You can get around this by using a different implementation of Ruby than the default, such as JRuby, but not all.

Phusion Passenger uses process based concurrency to handle a few requests concurrently, so, strictly speaking, is not "multithreaded," but is still concurrent.

This talk from Ruby MidWest 2011 has some good thoughts on getting multithreaded Ruby on Rails going.

like image 118
kjw0188 Avatar answered Sep 20 '22 17:09

kjw0188