r/ruby • u/Edthal01 • 12h 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.
2
u/andrevan 11h ago
There is also fibers
8
u/schneems Puma maintainer 12h 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.
1
u/mourad_dc 11h ago
If you want true parallel processing, you can use JRuby threads.
1
u/azimux 11h ago
Hey hey!
There's plenty of options for concurrency. If you need true concurrency for intense CPU-bound work, one option is separate processes if using CRuby. You could also use JRuby which doesn't have the GIL. Or you could use ractors in CRuby though they are experimental.
If what you're doing is heavily IO-bound (like database reads/writes) and not heavily CPU-bound (like in-memory number crunching) then really any concurrency option will work just fine in CRuby (separate processes, ractors, threads, fiber schedulers, or fibers.)
Most places I've worked when we had intense CPU-bound work to do in Ruby, which is extremely rare, then typically we've either used separate processes or even preemptively computed the results if the problem allows it.
At home, for the very rare cases where I did have true CPU-bound work to do, I've either used separate processes or used JRuby.
In short, I don't find concurrency that hard in Ruby though if I needed true parallelism in a seriously resource-constrained environment, then Ruby might not be the most natural choice compared to something with that as a design goal, depending.
1
u/yxhuvud 9h ago
What are your actual needs? Often it is not necessary to share memory and you can just spin up multiple workers.
2
u/CaptainKabob 9h ago
For your learning: you're describing C Ruby. JRuby does not have such limitations.
I think it's useful to learn why C Ruby has these limitations (ie the C) and also look at how other language runtimes, like Go, handle it (briefly, they have the same limitations, but structure their language features around them differently).
Also, on a social level, this is a topic where there are plenty of hot takes. And people are tired.
1
u/KevinSaison 2h ago
Concurrency and parallelism are not the same thing.
Concurrency is primarily about structuring work.
Parallelism is about executing work simultaneously.
Thread safety is about ensuring that simultaneous or interleaved work doesn't corrupt your program.