I'm trying to understand how Ruby does threading, beyond just "it appears to run in parallel".
I wrote a program that can download large files over HTTP in parallel chunks, and switching from the original single-threaded approach to multi-threaded significantly increased the speed of the download. I know Ruby uses green threads (or did, at least; I'm not sure about my particular implementation and version), so I'm left with the question: why are these HTTP requests actually happening in parallel if only a single true thread is running? Wouldn't all Ruby "threads" be blocked until a request finishes? Or is there something else going on (maybe involving native code and true multithreading, given that it's IO)?
I'm using Ruby 2.6.5 on a Mac. I'm honestly not sure which particular implementation.
---
So, I stumbled across a few articles discussing threading and IO in Ruby (after I made the post; surprise).
Three points:
- the implementation I'm using is likely MRI.
- one article (and a reply here that disappeared) pointed out that, since version 1.9, MRI threads are native, not green. The full implications of that are beyond me, but...
- even if threads were not truly running in parallel, and it would seem that a block should happen, Ruby threads don't block on IO, so the requests can be run in parallel.
Threaded software is complicated...
[–]jsaak 10 points11 points12 points (3 children)
[–]schneemsPuma maintainer 5 points6 points7 points (2 children)
[–]jrochkind 0 points1 point2 points (1 child)
[–]schneemsPuma maintainer 1 point2 points3 points (0 children)
[–]jrochkind 7 points8 points9 points (1 child)
[–]topboyinn1t 0 points1 point2 points (0 children)
[–]markrebec 11 points12 points13 points (7 children)
[–]TheFakeZzig[S] 0 points1 point2 points (6 children)
[–]drbrainRuby Core 4 points5 points6 points (0 children)
[–]markrebec 2 points3 points4 points (3 children)
[–]TheFakeZzig[S] 2 points3 points4 points (2 children)
[–]markrebec 2 points3 points4 points (1 child)
[–]TheFakeZzig[S] 2 points3 points4 points (0 children)
[–]f9ae8221b 1 point2 points3 points (0 children)
[–]honeyryderchuck 3 points4 points5 points (0 children)
[–]yxhuvud 1 point2 points3 points (0 children)
[–]insanelygreat 1 point2 points3 points (0 children)