3 ms·
I'm excited to see where this goes, but I must admit I'm conflicted on the idea of seeing async Ruby code. Async is great technically but one of the things I l
by freedomben 6y ago
I'm excited to see where this goes, but I must admit I'm conflicted on the idea of seeing async Ruby code. Async is great technically but one of the things I love about Ruby is the elegance and clarity. That tends to go out the window when async is introduced in many languages. async/await has helped a ton though, so hopefully Ruby will get something like that as we go.
I would just suggest that sometimes performance isn't worth sacrificing clarity. Obviously sometimes you have to (Big O can be an unforgiving beast) but not always.
- Lio 6y agoIt worth remembering that ruby code already handles async, and threads. For example ActiveRecord database requests have been non-blocking for many years. This new scheduler interface just gives us a nicer and lighter weight abstraction for handling it rather than, for example, using OS thread.
- jashmatthews 6y ago> ActiveRecord database requests have been non-blocking for many years. That's blocking IO with multi-threading. The executing thread stops and waits.
- Lio 6y agoYes, poor choice of words on my part. The IO calls themselves still block but each is in its own OS thread and GVL releases when the thread blocks. If you can only do parallel IO in a thread why not use a light weight fibre (green thread) instead of an OS thread? Hence this new scheduler and fibres.
- jashmatthews 6y ago> each is in its own OS thread and GVL releases when the thread blocks. The GVL isn't automatically released when a thread blocks on IO. Each bit of native code performing IO has to explicitly release it like in the pg gem here: https://github.com/ged/ruby-pg/blob/fb465855ce1dd12cf7eb69c92af222052aa956ce/ext/gvl_wrappers.h#L4 https://github.com/ged/ruby-pg/blob/fb465855ce1dd12cf7eb69c9... If you don't need high concurrency then using threads is simpler.
- adrianpike 6y ago> For example ActiveRecord database requests have been non-blocking for many years. Right, but it still blocks the entire calling thread, no? Been a few years since I've been deep into Rails.
- Lio 6y agoAh, I see the confusion in what I’ve said. Yes the IO calls themselves are blocking but you can easily wrap them in separate OS threads. The GVL will release when the IO call blocks allowing another OS thread, including the parent, to continue. That allows you to make blocking calls in parallel but is heavier weight than using the new scheduler and fibres for those parallel IO calls. In the case of ActiveRecord I think it’s down to the database driver implementation, anyone who knows care to comment?
- ljm 6y agoTime will tell, but this could be beneficial for existing libraries like concurrent-rb. And, maybe it becomes more useful for using ruby in smaller services outside of the Rails context, especially when the go-to solution for a bunch of problems is to run a separate copy of your server in 'worker mode'. Even with Rails, not relying on Delayed Job or Sidekiq by default would be nice. Of course, I'm thinking about Ractor there too.
- deleted 6y ago[deleted]
- nitrogen 6y agoBefore I learned Rails I used EventMachine, which was very "async". Everything was a chained callback. So Ruby has had the option of this complexity for a while.
- jashmatthews 6y agoUsing FiberScheduler allows you to use async IO without the additional complexity of callbacks.