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

9 Upvotes

27 comments sorted by

View all comments

2

u/azimux 2d 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 2d 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.

3

u/azimux 2d 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 2d ago

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