On this page
Ractor
Ractor.new creates a new Ractor, which can run in parallel with other ractors.
# The simplest ractor r = Ractor.new {puts "I am in Ractor!"} r.join # wait for it to finish # Here, "I am in Ractor!" is printed
Ractors do not share all objects with each other. There are two main benefits to this: across ractors, thread-safety concerns such as data-races and race-conditions are not possible. The other benefit is parallelism.
To achieve this, object sharing is limited across ractors. Unlike in threads, ractors can’t access all the objects available in other ractors. For example, objects normally available through variables in the outer scope are prohibited from being used across ractors.
a = 1 r = Ractor.new {puts "I am in Ractor! a=#{a}"} # fails immediately with # ArgumentError (can not isolate a Proc because it accesses outer variables (a).)
The object must be explicitly shared:
a = 1 r = Ractor.new(a) { |a1| puts "I am in Ractor! a=#{a1}"}
On CRuby (the default implementation), the Global Virtual Machine Lock (GVL) is held per ractor, so ractors can run in parallel. This is unlike the situation with threads on CRuby.
Instead of accessing shared state, objects should be passed to and from ractors by sending and receiving them as messages.
a = 1 r = Ractor.new do a_in_ractor = receive # receive blocks the Thread until our default port gets sent a message puts "I am in Ractor! a=#{a_in_ractor}" end r.send(a) # pass it r.join # Here, "I am in Ractor! a=1" is printed
In addition to that, any arguments passed to Ractor.new are passed to the block and available there as if received by Ractor.receive, and the last block value can be received with Ractor#value.
Shareable and unshareable objects
When an object is sent to a ractor, it’s important to understand whether the object is shareable or unshareable. Most Ruby objects are unshareable objects. Even frozen objects can be unshareable if they contain (through their instance variables) unfrozen objects.
Shareable objects are those which can be used by several ractors at once without compromising thread-safety, for example numbers, true and false. Ractor.shareable? allows you to check this, and Ractor.make_shareable tries to make the object shareable if it’s not already and gives an error if it can’t do it.
Ractor.shareable?(1) #=> true -- numbers and other immutable basic values are shareable Ractor.shareable?('foo') #=> false, unless the string is frozen due to # frozen_string_literal: true Ractor.shareable?('foo'.freeze) #=> true Ractor.shareable?([Object.new].freeze) #=> false, inner object is unfrozen ary = ['hello', 'world'] ary.frozen? #=> false ary[0].frozen? #=> false Ractor.make_shareable(ary) ary.frozen? #=> true ary[0].frozen? #=> true ary[1].frozen? #=> true
When a shareable object is sent via send, no additional processing occurs on it and it becomes usable by both ractors. When an unshareable object is sent, it can be either copied or moved. Copying is the default, and it copies the object fully by deep cloning (Object#clone) the non-shareable parts of its structure.
data = ['foo'.dup, 'bar'.freeze] r = Ractor.new do data2 = Ractor.receive puts "In ractor: #{data2.object_id}, #{data2[0].object_id}, #{data2[1].object_id}" end r.send(data) r.join puts "Outside : #{data.object_id}, #{data[0].object_id}, #{data[1].object_id}"
This will output something like:
In ractor: 8, 16, 24 Outside : 32, 40, 24
Note that the object ids of the array and the non-frozen string inside the array have changed in the ractor because they are different objects. The second array’s element, which is a shareable frozen string, is the same object.
Deep cloning of objects may be slow, and sometimes impossible. Alternatively, move: true may be used during sending. This will move the unshareable object to the receiving ractor, making it inaccessible to the sending ractor.
data = ['foo', 'bar'] r = Ractor.new do data_in_ractor = Ractor.receive puts "In ractor: #{data_in_ractor.object_id}, #{data_in_ractor[0].object_id}" end r.send(data, move: true) r.join puts "Outside: moved? #{Ractor::MovedObject === data}" puts "Outside: #{data.inspect}"
This will output:
In ractor: 100, 120 Outside: moved? true test.rb:9:in `method_missing': can not send any methods to a moved object (Ractor::MovedError)
Notice that even inspect and more basic methods like __id__ are inaccessible on a moved object.
Class and Module objects are shareable and their class/module definitions are shared between ractors. Ractor objects are also shareable. All operations on shareable objects are thread-safe across ractors. Defining mutable, shareable objects in Ruby is not possible, but C extensions can introduce them.
It is prohibited to access (get) instance variables of shareable objects in other ractors if the values of the variables aren’t shareable. This can occur because modules/classes are shareable, but they can have instance variables whose values are not. In non-main ractors, it’s also prohibited to set instance variables on classes/modules (even if the value is shareable).
class C class << self attr_accessor :tricky end end C.tricky = "unshareable".dup r = Ractor.new(C) do |cls| puts "I see #{cls}" puts "I can't see #{cls.tricky}" cls.tricky = true # doesn't get here, but this would also raise an error end r.join # I see C # can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
Ractors can access constants if they are shareable. The main Ractor is the only one that can access non-shareable constants.
GOOD = 'good'.freeze BAD = 'bad'.dup r = Ractor.new do puts "GOOD=#{GOOD}" puts "BAD=#{BAD}" end r.join # GOOD=good # can not access non-shareable objects in constant Object::BAD by non-main Ractor. (NameError) # Consider the same C class from above r = Ractor.new do puts "I see #{C}" puts "I can't see #{C.tricky}" end r.join # I see C # can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
See also the description of # shareable_constant_value pragma in Comments syntax explanation.
Ractors vs threads
Each ractor has its own main Thread. New threads can be created from inside ractors (and, on CRuby, they share the GVL with other threads of this ractor).
r = Ractor.new do a = 1 Thread.new {puts "Thread in ractor: a=#{a}"}.join end r.join # Here "Thread in ractor: a=1" will be printed
Note on code examples
In the examples below, sometimes we use the following method to wait for ractors to make progress or finish.
def wait sleep(0.1) end
This is **only for demonstration purposes** and shouldn’t be used in a real code. Most of the time, join is used to wait for ractors to finish and Ractor.receive is used to wait for messages.
Reference
See Ractor design doc for more details.
Class methods 15
Instance methods 15
Ruby version changes 12
-
4.0
-
Ractor::Portclass was added for a new synchronization mechanism to communicate between Ractors. [Feature #21262]Exampleport1 = Ractor::Port.new port2 = Ractor::Port.new Ractor.new port1, port2 do |port1, port2| port1 << 1 port2 << 11 port1 << 2 port2 << 12 end 2.times{ p port1.receive } #=> 1, 2 2.times{ p port2.receive } #=> 11, 12
Ractor::Portprovides the following methods:As a result,
Ractor.yieldandRactor#takewere removed. -
Ractor#joinandRactor#valuewere added to wait for the termination of aRactor. These are similar toThread#joinandThread#value. -
Ractor#monitorandRactor#unmonitorwere added as low-level interfaces used internally to implementRactor#join. -
Ractor.selectnow only accepts Ractors and Ports. If Ractors are given, it returns when aRactorterminates. -
Ractor#default_portwas added. EachRactorhas a default port, which is used byRactor.send,Ractor.receive. -
Ractor#close_incomingandRactor#close_outgoingwere removed. -
Ractor.shareable_procandRactor.shareable_lambdaare introduced to make shareableProcor lambda. [Feature #21550], [Feature #21557]
-
-
3.4
-
requireinRactoris allowed. The requiring process will be run on the mainRactor.Ractor._require(feature)is added to run requiring process on the mainRactor. [Feature #20627] -
Ractor.main?is added. [Feature #20627] -
Ractor.[]andRactor.[]=are added to access the ractor local storage of the currentRactor. [Feature #20715] -
Ractor.store_if_absent(key){ init }is added to initialize ractor local variables in thread-safety. [Feature #20875]
-
-
3.0
-
New class added to enable parallel execution. See ractor.md for more details.
-