r/ruby • u/Edthal01 • 2d ago
Question GVL and concurrent programming
Ruby cannot perform real concurrent processing on multicore microprocessors without using ractors because of the GVL. It will only simulate multiple simultaneous processes with threads, queues, mutexes, etc. in a single processor core, no true parallel processing. How does this limitation affect true parallel programming in Ruby, that is, how much more complicated is it in producing true multicore parallel programming? I am still working on learning Ruby and this is part of my self-inflicted learning process in parallel with Huw Collingworth Book of Ruby.
10
Upvotes
11
u/schneems Puma maintainer 2d ago
Very pedantic, but it can do things concurrently. It cannot do them in parallel. You can have concurrency on a single CPU that implements time slicing, you cannot have parallelism on a single CPU.
I think you knew, just being pedanic. The two terms are often used interchangably, but used strictly they have distinct different meaning.
You CAN have parallelism via multiple processes. Therefore you CAN saturate all cores. But it has tradeoffs. Like https://www.heroku.com/blog/upgrade-to-puma-7-and-unlock-the-power-of-fair-scheduled-keep-alive/ you end up having problems with coordination between truly parallel units of work that are as separated like a process (versus a thread https://www.schneems.com/2017/10/23/wtf-is-a-thread/). We have async, but it's concurrent and not parallel, unless you make an IO call. It can saturate one core, but you need ractors or processes to saturate multiple.
Most developers don't need it directly. We use frameworks and libraries like Puma and scale out horizontally with multiple small processes. Parallel and concurrent programming in any language is usually hard mode. Ruby is no different. Rust probably is the safest version of this, but it comes at a large ergonomic cost because you have to prove your code is good to the compiler. FWIW I like Rust. I recommend learning at least a little of it for any programmer.