Reactor, fiber scheduler and async in Ruby 3.0

Jacob Carlborg doob at me.com
Thu Dec 31 14:53:52 UTC 2020


In Ruby 3.0, support for reactor, fiber scheduler and async has been added.

async seems to be implemented in library code. The existing IO related 
methods in the core/standard library has been updated to be fiber 
scheduler aware. That basically means that if there is no fiber 
scheduler set, the IO operations will be blocking. If a fiber scheduler 
has been set, they will instead be non-blocking. A lot of existing code 
and gems will just work without having to be adopted for async.

Here's an example:

require 'async'
require 'net/http'
require 'uri'

Async do
   ["ruby", "rails", "async"].each do |topic|
     Async do
       Net::HTTP.get(URI "https://www.google.com/search?q=#{topic}")
     end
   end
end

IO#read and IO#write are two of the methods that have been made fiber 
scheduler aware. Net::HTTP#get uses these methods under the hood and 
therefore will be non-blocking inside the Async block. See [1] for more 
details.

BTW, Zig uses the same idea with the underlying IO functions that can 
adopt and become non-blocking.

https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list