Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the current state of affairs on threading, concurrency and forked processes, in Ruby on Rails?

Ruby on Rails does not do multithreaded request-responses very well, or at least, ActiveRecord doesn't.

The notion of only one request-response active at the same time can be a hassle when creating web applications which fork off a shell-command that takes long to finish.

What I'd like are some of your views on these kinds of setups? Is Rails maybe not a good fit for some applications?

Also, what are the current state of affairs in regard to concurrency in Ruby on Rails? What are the best practices. Are there workarounds to the shortcomings?

like image 228
Hans Sjunnesson Avatar asked Sep 24 '08 19:09

Hans Sjunnesson


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.

How does Ruby handle concurrency?

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).

Does Ruby have concurrency?

Ruby Fibers let you achieve cooperative concurrency within a single thread. This means that fibers are not preempted and the program itself must do the scheduling. Because the programmer controls when fibers start and stop, it is much easier to avoid race conditions.

What are threads and processes in Ruby?

Multi-threading is the most useful property of Ruby which allows concurrent programming of two or more parts of the program for maximizing the utilization of CPU. Each part of a program is called Thread. So, in other words, threads are lightweight processes within a process.


2 Answers

Rails currently doesn't handle concurrent requests within a single MRI (Matz Ruby Interpreter) Ruby process. Each request is essentally wrapped with a giant mutex. A lot of work has gone into making the forthcoming Rails 2.2 thread-safe, but you're not going to get a lot of benefit from this when running under Ruby 1.8x. I can't comment on whether Ruby 1.9 will be different because I'm not very familiar with it, but probably not I'd have thought.

One area that does look very promising in this regard is running Rails using JRuby, because the JVM is generally acknowledged as being good at multi-threading. Arun Gupta from Sun Microsystems gave some interesting performance figures on this setup at RailsConf Europe recently.

like image 199
John Topley Avatar answered Sep 28 '22 17:09

John Topley


Neverblock allows for non blocking functionality without modifying the way you write programs. It really is an exciting looking project, and was backported to work on Ruby 1.8.x (it relies on Ruby 1.9's fibers). It works with both PostgreSQL and MySQL to perform non-blocking queries. The benchmarks are crazy...

like image 32
Zach Avatar answered Sep 28 '22 16:09

Zach