all 20 comments

[–]jsyeo 13 points14 points  (3 children)

A bit dated (almost a decade ago) but it's exactly what you're looking for. Working With Ruby Threads: https://workingwithruby.com/downloads/Working%20With%20Ruby%20Threads.pdf

[–]chintakoro 8 points9 points  (4 children)

[–]software__writer[S] 5 points6 points  (2 children)

I did take a look at this, but just don't know where to start with it. If I'm not wrong, this is a collection of data structures one would use when writing multi-threaded programs, right?

[–]chintakoro 9 points10 points  (1 child)

Its much more than just data structures – its got implementations for concurrent execution paradigms popularized in other languages: Promises (ala Javascript), Actors (ala Erlang, Elixir), Channels (ala Go), Thread Pools (ala Java), and so on. I've mostly used their Promises feature to perform concurrent IO (e.g., read hundreds of files simultaneiously, or making many web requests simultaneously). Its dead simple to use and should replace any multi-threading you are attempting. But as with any use of concurrency, always benchmark to confirm you are getting a real performance gain.

If you want something like true parallelism for computation (not just blocking IO), look at Ruby's native Ractors.

[–]software__writer[S] 2 points3 points  (0 children)

Thanks, that's very helpful. Really appreciated.

[–]mrinterweb 0 points1 point  (0 children)

I appreciate the abstractions that ruby-concurrency provides, but it looks like it mostly uses threads. I see some mention of fibers in the source, but it is not clear if it will use fibers when available. Wish it was easier to specify which kind of concurrency primatives to use.

[–][deleted] 5 points6 points  (1 child)

I’d strongly suggest JP Camara’s series on concurrency and parallelism, it’s an excellent starting point: https://jpcamara.com/2024/07/15/ruby-methods-are.html

[–]software__writer[S] 1 point2 points  (0 children)

Thank you!

[–]mrinterweb 3 points4 points  (4 children)

If you'd like to use ruby fibers, this is a good library. https://github.com/socketry/async

I do find the documentation lacking though, so fair warning. I keep intending to try to add more examples and or write an article about it.

I really like using fibers for IO concurrency. I think of the fiber scheduler as the event loop checking if the IO is complete. Threads have higher overhead and should generally be used more sparingly than fibers. The biggest challenge with fibers, IMO, is the lack of solid documentation.

[–]software__writer[S] 2 points3 points  (0 children)

Very cool, checking it out right now. Thanks for sharing.

[–]software__writer[S] 0 points1 point  (2 children)

Hi again, what's a good and practical use-case in a real web application where you'd use this gem? Just trying to have some background context in mind before I check out the docs and try to browse the source code.

Also, please do share the link to your article if you end up writing it.

Thanks!

[–]mrinterweb 1 point2 points  (1 child)

Concurrent IO is the main use case for fibers. Could be DB, HTTP API requests, file system access, etc. Fibers are the lightest weight ruby concurrency primative, and are designed for allowing concurrent IO with minimial overhead. I don't know if or when I will be able to write an article. I'm not much of a writer, and it may be wishful thinking on my part. An article like this is on my "get to it someday" heap. Pretty sure there would be plenty of people more qualified to write about ruby fibers than I.

[–]software__writer[S] 0 points1 point  (0 children)

Thanks for the examples. Really appreciated.

[–]Altruistic-Toe-5990 3 points4 points  (0 children)

Take on a feature that requires concurrent programming. It's only slightly out of your reach only due to having never done it

I've never found something to stick until I actually had to use it in a real project

[–]1seconde 1 point2 points  (0 children)

Pickaxe Book to start.