r/ruby • • 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.

7 Upvotes

21 comments sorted by

2

u/andrevan 11h ago

There is also fibers

1

u/Edthal01 11h ago

As a learner that seems the way for me to go.

1

u/azimux 7h ago

Fibers are fun to learn/use but if you're specifically interested in true concurrency, then it's probably worth mentioning that fibers don't offer true concurrency. That is, they're good for IO-bound work but not CPU-bound work. So just depends on what you're up to or what you're interested in learning about.

1

u/Edthal01 3h ago

I don't need to think about more than 1 core, just how to learn about concurrent processes.

1

u/andrevan 11h ago

i use em in production!

8

u/schneems Puma maintainer 12h ago

real concurrent processing

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.

true parallel programming

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.

how much more complicated is it in producing true multicore parallel programming?

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/Edthal01 11h ago

As I understood: Parallel in separate cores more complicated than concurrent in single core.

2

u/schneems Puma maintainer 11h ago

That's usually true because parallelism means things happening at literally the same time, and most people think about code sequentially and are bad at remembering external events that might affect what they're doing. Race conditions and deadlocks and friends are very difficult to find, and sometimes.

Concurrent programming is easier but can still have surprises. It also depends on the concurrency model. Technically the threading model with MRI's GIL/GVL means that only one Ruby thread can be executing at any given time, but writing code where the interpreter could switch to run another thread inbewtween EVERY virtual machine instruction ALSO has some difficult to reason about edge cases and bugs. In practice I would treat both as equally difficult. There are some patterns like "actor model" and "producer consumer loops" that can make changes easier to reason about, but they're still all more difficult to work with than regular, single threaded, sequential code. (this is true in all languages, not just Ruby).

1

u/yxhuvud 9h ago

They also didn't specify the actual needs in terms of sharing, so the simplest solution is to just spin up multiple workers and not share memory.

1

u/KimJongIlLover 4h ago

What about elixir? Offers true concurrency but doesn't give you the rust stare.

1

u/mourad_dc 11h ago

If you want true parallel processing, you can use JRuby threads.

1

u/Edthal01 11h ago

I am using Manjaro Linux and will need to install jruby first. We'll see what happens.

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/Edthal01 11h ago

As I originally posted, I am studying concurrent/parallel processing in Ruby in parallel with learning Ruby, and was making an obervation, partially out of inexperience.

2

u/azimux 11h ago

Hi! I was trying to answer this question in your post:

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 suppose there might be confusion in the intent behind my answer or your intent posing the question. To recap my answer to my porentially wrong understanding of your question: it's not that complex for me. It is rare that it comes up but when it does I use separate processes, JRuby, or preemptively compute the data if I can instead of on-demand. I'd also be fine using Ractors for this purpose personally, though they are experimental.

But yeah whoops if we're talking past each other. Hope you're having fun learning concurrency/Ruby!

1

u/Edthal01 11h ago

Ah, clarity. Intellectual curiosity on my part, pragmatic experience on yours.

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.

1

u/Edthal01 3h ago

my need is to learn about concurrent programming in Ruby. An intellectual activity.

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/h0rst_ 4h ago

I think it's useful to learn why C Ruby has these limitations (ie the C)

Are you trying to say that this limitation exists in CRuby because it is written in C? That's not why the GVL exists.

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.