Ractor
Ractor is experimental, and the behavior may change in future versions
of Ruby! Also there are many implementation issues.
async is a solution for concurrency. Ractor is for parallelism.
ractors = (1..6).map do |x|
Ractor.new(x) do |id|
result = (1..1_000_000_000).reduce(:+)
Ractor.yield([id, result])
end
end
puts ractors.map(&:take).sort_by(&:first).map(&:last).inspect
Message Passing
Ractor also follows the actor model(ruby actor?). Each actor mantains inner state, and communicates using messages.
actor = Ractor.new do
state = 0
loop do
message = Ractor.receive
case message
when :increment
state += 1
Ractor.yield(state)
when :get_state
Ractor.yield(state)
when :exit
break
end
end
end
actor.send(:increment)
puts actor.take
actor.send(:get_state)
puts actor.take
actor.send(:exit)