Ruby Concurrency
GIL
https://yehudakatz.com/2010/08/14/threads-in-ruby-enough-already/
- Can't run Ruby code in the same process in multiple threads at the same time
- But when a thread blocks on I/O, another thread can run
- For example, you can run multiple HTTP requests in parallel, or multiple database queries in parallel
- You do need one Ruby process per CPU core. But with multithreading you should not need more than one.
Processes
- You can run separate Ruby processes which can run in parallel
- Web servers like Puma allow multiple processes
Threads
- Web servers like Puma allow multiple threads
- Rails has been thread-safe since 2.2
Fibers
- Added in Ruby 1.9
- A coroutine mechanism
- Fiber.new, yield, resume
Async Fibers/Scheduler
- For I/O heavy tasks
- Like Async/Await in JS
- Can't use multi-core
- https://github.com/socketry/falcon
- https://www.youtube.com/watch?v=Y29SSOS4UOc
Ractor (previously known as guilds)
- For multi-core
- For CPU-intensive tasks
- Isolated object space. Nothing mutable shared. Immutable things can be shared
- Interpreter lock will now be per Ractor, for threads within the Ractor
- https://www.youtube.com/watch?v=0kM7yFM6Dao&list=PLbHJudTY1K0cyDs1ZwFLzlfQqvkDKt17N&index=12
Slapdash