# # Ractor is an Actor-model abstraction for Ruby that provides thread-safe # parallel execution. # # Ractor.new makes a new Ractor, which can run in parallel. # # # The simplest ractor # r = Ractor.new {puts "I am in Ractor!"} # r.take # 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. For example, unlike # in threads, ractors can't access all the objects available in other ractors. # Even 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), Global Virtual Machine Lock (GVL) is # held per ractor, so ractors can perform in parallel without locking each # other. 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 until somebody passes a message # puts "I am in Ractor! a=#{a_in_ractor}" # end # r.send(a) # pass it # r.take # # Here, "I am in Ractor! a=1" is printed # # There are two pairs of methods for sending/receiving messages: # # * Ractor#send and Ractor.receive for when the *sender* knows the receiver # (push); # * Ractor.yield and Ractor#take for when the *receiver* knows the sender # (pull); # # # 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 is sent outside of the ractor as if sent by Ractor.yield. # # A little demonstration of a classic ping-pong: # # server = Ractor.new(name: "server") do # puts "Server starts: #{self.inspect}" # puts "Server sends: ping" # Ractor.yield 'ping' # The server doesn't know the receiver and sends to whoever interested # received = Ractor.receive # The server doesn't know the sender and receives from whoever sent # puts "Server received: #{received}" # end # # client = Ractor.new(server) do |srv| # The server is sent to the client, and available as srv # puts "Client starts: #{self.inspect}" # received = srv.take # The client takes a message from the server # puts "Client received from " \ # "#{srv.inspect}: #{received}" # puts "Client sends to " \ # "#{srv.inspect}: pong" # srv.send 'pong' # The client sends a message to the server # end # # [client, server].each(&:take) # Wait until they both finish # # This will output something like: # # Server starts: # # Server sends: ping # Client starts: # # Client received from #: ping # Client sends to #: pong # Server received: pong # # Ractors receive their messages via the *incoming port*, and send them to the # *outgoing port*. Either one can be disabled with Ractor#close_incoming and # Ractor#close_outgoing, respectively. When a ractor terminates, its ports are # closed automatically. # # ## Shareable and unshareable objects # # When an object is sent to and from 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 threads 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 or Ractor.yield), no additional # processing occurs on it. It just becomes usable by both ractors. When an # unshareable object is sent, it can be either *copied* or *moved*. The first is # the default, and it copies the object fully by deep cloning (Object#clone) the # non-shareable parts of its structure. # # data = ['foo', '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.take # puts "Outside : #{data.object_id}, #{data[0].object_id}, #{data[1].object_id}" # # This will output something like: # # In ractor: 340, 360, 320 # Outside : 380, 400, 320 # # 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.take # 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__`) is # inaccessible on a moved object. # # Class and Module objects are shareable so the class/module definitions are # shared between ractors. Ractor objects are also shareable. All operations on # shareable objects are thread-safe, so the thread-safety property will be kept. # We can not define mutable shareable objects in Ruby, 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.take # # 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.take # # 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.take # # 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](rdoc-ref:syntax/comments.rdoc) 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.take # # 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 that are not currently blocked to finish (or to make progress). # # def wait # sleep(0.1) # end # # It is **only for demonstration purposes** and shouldn't be used in a real # code. Most of the time, #take is used to wait for ractors to finish. # # ## Reference # # See [Ractor design doc](rdoc-ref:ractor.md) for more details. # class Ractor # # Returns the number of Ractors currently running or blocking (waiting). # # Ractor.count #=> 1 # r = Ractor.new(name: 'example') { Ractor.yield(1) } # Ractor.count #=> 2 (main + example ractor) # r.take # wait for Ractor.yield(1) # r.take # wait until r will finish # Ractor.count #=> 1 # def self.count: () -> Integer # # Returns the currently executing Ractor. # # Ractor.current #=> # # def self.current: () -> untyped # # returns main ractor # def self.main: () -> untyped # # Make `obj` shareable between ractors. # # `obj` and all the objects it refers to will be frozen, unless they are already # shareable. # # If `copy` keyword is `true`, it will copy objects before freezing them, and # will not modify `obj` or its internal objects. # # Note that the specification and implementation of this method are not mature # and may be changed in the future. # # obj = ['test'] # Ractor.shareable?(obj) #=> false # Ractor.make_shareable(obj) #=> ["test"] # Ractor.shareable?(obj) #=> true # obj.frozen? #=> true # obj[0].frozen? #=> true # # # Copy vs non-copy versions: # obj1 = ['test'] # obj1s = Ractor.make_shareable(obj1) # obj1.frozen? #=> true # obj1s.object_id == obj1.object_id #=> true # obj2 = ['test'] # obj2s = Ractor.make_shareable(obj2, copy: true) # obj2.frozen? #=> false # obj2s.frozen? #=> true # obj2s.object_id == obj2.object_id #=> false # obj2s[0].object_id == obj2[0].object_id #=> false # # See also the "Shareable and unshareable objects" section in the Ractor class # docs. # def self.make_shareable: [T] (T obj, ?copy: boolish) -> T # # Create a new Ractor with args and a block. # # The given block (Proc) will be isolated (can't access any outer variables). # `self` inside the block will refer to the current Ractor. # # r = Ractor.new { puts "Hi, I am #{self.inspect}" } # r.take # # Prints "Hi, I am #" # # Any `args` passed are propagated to the block arguments by the same rules as # objects sent via #send/Ractor.receive. If an argument in `args` is not # shareable, it will be copied (via deep cloning, which might be inefficient). # # arg = [1, 2, 3] # puts "Passing: #{arg} (##{arg.object_id})" # r = Ractor.new(arg) {|received_arg| # puts "Received: #{received_arg} (##{received_arg.object_id})" # } # r.take # # Prints: # # Passing: [1, 2, 3] (#280) # # Received: [1, 2, 3] (#300) # # Ractor's `name` can be set for debugging purposes: # # r = Ractor.new(name: 'my ractor') {}; r.take # p r # #=> # # def self.new: (*untyped args, ?name: string) { (?) -> untyped } -> Ractor # # Receive a message from the incoming port of the current ractor (which was sent # there by #send from another ractor). # # r = Ractor.new do # v1 = Ractor.receive # puts "Received: #{v1}" # end # r.send('message1') # r.take # # Here will be printed: "Received: message1" # # Alternatively, the private instance method `receive` may be used: # # r = Ractor.new do # v1 = receive # puts "Received: #{v1}" # end # r.send('message1') # r.take # # This prints: "Received: message1" # # The method blocks if the queue is empty. # # r = Ractor.new do # puts "Before first receive" # v1 = Ractor.receive # puts "Received: #{v1}" # v2 = Ractor.receive # puts "Received: #{v2}" # end # wait # puts "Still not received" # r.send('message1') # wait # puts "Still received only one" # r.send('message2') # r.take # # Output: # # Before first receive # Still not received # Received: message1 # Still received only one # Received: message2 # # If close_incoming was called on the ractor, the method raises # Ractor::ClosedError if there are no more messages in the incoming queue: # # Ractor.new do # close_incoming # receive # end # wait # # in `receive': The incoming port is already closed => # (Ractor::ClosedError) # def self.receive: () -> untyped # # Receive only a specific message. # # Instead of Ractor.receive, Ractor.receive_if can be given a pattern (or any # filter) in a block and you can choose the messages to accept that are # available in your ractor's incoming queue. # # r = Ractor.new do # p Ractor.receive_if{|msg| msg.match?(/foo/)} #=> "foo3" # p Ractor.receive_if{|msg| msg.match?(/bar/)} #=> "bar1" # p Ractor.receive_if{|msg| msg.match?(/baz/)} #=> "baz2" # end # r << "bar1" # r << "baz2" # r << "foo3" # r.take # # This will output: # # foo3 # bar1 # baz2 # # If the block returns a truthy value, the message is removed from the incoming # queue and returned. Otherwise, the message remains in the incoming queue and # the next messages are checked by the given block. # # If there are no messages left in the incoming queue, the method will block # until new messages arrive. # # If the block is escaped by break/return/exception/throw, the message is # removed from the incoming queue as if a truthy value had been returned. # # r = Ractor.new do # val = Ractor.receive_if{|msg| msg.is_a?(Array)} # puts "Received successfully: #{val}" # end # # r.send(1) # r.send('test') # wait # puts "2 non-matching sent, nothing received" # r.send([1, 2, 3]) # wait # # Prints: # # 2 non-matching sent, nothing received # Received successfully: [1, 2, 3] # # Note that you can not call receive/receive_if in the given block recursively. # You should not do any tasks in the block other than message filtration. # # Ractor.current << true # Ractor.receive_if{|msg| Ractor.receive} # #=> `receive': can not call receive/receive_if recursively (Ractor::Error) # def self.receive_if: () { (untyped) -> boolish } -> untyped # # alias self.recv self.receive # # Wait for any ractor to have something in its outgoing port, read from this # ractor, and then return that ractor and the object received. # # r1 = Ractor.new {Ractor.yield 'from 1'} # r2 = Ractor.new {Ractor.yield 'from 2'} # # r, obj = Ractor.select(r1, r2) # # puts "received #{obj.inspect} from #{r.inspect}" # # Prints: received "from 1" from # # # But could just as well print "from r2" here, either prints could be first. # # If one of the given ractors is the current ractor, and it is selected, `r` # will contain the `:receive` symbol instead of the ractor object. # # r1 = Ractor.new(Ractor.current) do |main| # main.send 'to main' # Ractor.yield 'from 1' # end # r2 = Ractor.new do # Ractor.yield 'from 2' # end # # r, obj = Ractor.select(r1, r2, Ractor.current) # puts "received #{obj.inspect} from #{r.inspect}" # # Could print: received "to main" from :receive # # If `yield_value` is provided, that value may be yielded if another ractor is # calling #take. In this case, the pair `[:yield, nil]` is returned: # # r1 = Ractor.new(Ractor.current) do |main| # puts "Received from main: #{main.take}" # end # # puts "Trying to select" # r, obj = Ractor.select(r1, Ractor.current, yield_value: 123) # wait # puts "Received #{obj.inspect} from #{r.inspect}" # # This will print: # # Trying to select # Received from main: 123 # Received nil from :yield # # `move` boolean flag defines whether yielded value will be copied (default) or # moved. # def self.select: (*Ractor ractors, ?move: boolish, ?yield_value: untyped) -> [ Ractor | Symbol, untyped ] # # Checks if the object is shareable by ractors. # # Ractor.shareable?(1) #=> true -- numbers and other immutable basic values are frozen # Ractor.shareable?('foo') #=> false, unless the string is frozen due to # frozen_string_literal: true # Ractor.shareable?('foo'.freeze) #=> true # # See also the "Shareable and unshareable objects" section in the Ractor class # docs. # def self.shareable?: (untyped obj) -> bool # # Send a message to the current ractor's outgoing port to be accepted by #take. # # r = Ractor.new {Ractor.yield 'Hello from ractor'} # puts r.take # # Prints: "Hello from ractor" # # This method is blocking, and will return only when somebody consumes the sent # message. # # r = Ractor.new do # Ractor.yield 'Hello from ractor' # puts "Ractor: after yield" # end # wait # puts "Still not taken" # puts r.take # # This will print: # # Still not taken # Hello from ractor # Ractor: after yield # # If the outgoing port was closed with #close_outgoing, the method will raise: # # r = Ractor.new do # close_outgoing # Ractor.yield 'Hello from ractor' # end # wait # # `yield': The outgoing-port is already closed (Ractor::ClosedError) # # The meaning of the `move` argument is the same as for #send. # def self.yield: (untyped obj, ?move: boolish) -> untyped public # # alias << send # # get a value from ractor-local storage # def []: (interned sym) -> untyped # # set a value in ractor-local storage # def []=: [T] (interned sym, T val) -> T # # Closes the incoming port and returns whether it was already closed. All # further attempts to Ractor.receive in the ractor, and #send to the ractor will # fail with Ractor::ClosedError. # # r = Ractor.new {sleep(500)} # r.close_incoming #=> false # r.close_incoming #=> true # r.send('test') # # Ractor::ClosedError (The incoming-port is already closed) # def close_incoming: () -> bool # # Closes the outgoing port and returns whether it was already closed. All # further attempts to Ractor.yield in the ractor, and #take from the ractor will # fail with Ractor::ClosedError. # # r = Ractor.new {sleep(500)} # r.close_outgoing #=> false # r.close_outgoing #=> true # r.take # # Ractor::ClosedError (The outgoing-port is already closed) # def close_outgoing: () -> bool # # def inspect: () -> String # # The name set in Ractor.new, or `nil`. # def name: () -> String? # # Send a message to a Ractor's incoming queue to be accepted by Ractor.receive. # # r = Ractor.new do # value = Ractor.receive # puts "Received #{value}" # end # r.send 'message' # # Prints: "Received: message" # # The method is non-blocking (will return immediately even if the ractor is not # ready to receive anything): # # r = Ractor.new {sleep(5)} # r.send('test') # puts "Sent successfully" # # Prints: "Sent successfully" immediately # # An attempt to send to a ractor which already finished its execution will raise # Ractor::ClosedError. # # r = Ractor.new {} # r.take # p r # # "#" # r.send('test') # # Ractor::ClosedError (The incoming-port is already closed) # # If close_incoming was called on the ractor, the method also raises # Ractor::ClosedError. # # r = Ractor.new do # sleep(500) # receive # end # r.close_incoming # r.send('test') # # Ractor::ClosedError (The incoming-port is already closed) # # The error is raised immediately, not when the ractor tries to receive # # If the `obj` is unshareable, by default it will be copied into the receiving # ractor by deep cloning. If `move: true` is passed, the object is *moved* into # the receiving ractor and becomes inaccessible to the sender. # # r = Ractor.new {puts "Received: #{receive}"} # msg = 'message' # r.send(msg, move: true) # r.take # p msg # # This prints: # # Received: message # in `p': undefined method `inspect' for # # # All references to the object and its parts will become invalid to the sender. # # r = Ractor.new {puts "Received: #{receive}"} # s = 'message' # ary = [s] # copy = ary.dup # r.send(ary, move: true) # # s.inspect # # Ractor::MovedError (can not send any methods to a moved object) # ary.class # # Ractor::MovedError (can not send any methods to a moved object) # copy.class # # => Array, it is different object # copy[0].inspect # # Ractor::MovedError (can not send any methods to a moved object) # # ...but its item was still a reference to `s`, which was moved # # If the object is shareable, `move: true` has no effect on it: # # r = Ractor.new {puts "Received: #{receive}"} # s = 'message'.freeze # r.send(s, move: true) # s.inspect #=> "message", still available # def send: (untyped obj, ?move: boolish) -> Ractor # # Get a message from the ractor's outgoing port, which was put there by # Ractor.yield or at ractor's termination. # # r = Ractor.new do # Ractor.yield 'explicit yield' # 'last value' # end # puts r.take #=> 'explicit yield' # puts r.take #=> 'last value' # puts r.take # Ractor::ClosedError (The outgoing-port is already closed) # # The fact that the last value is also sent to the outgoing port means that # `take` can be used as an analog of Thread#join ("just wait until ractor # finishes"). However, it will raise if somebody has already consumed that # message. # # If the outgoing port was closed with #close_outgoing, the method will raise # Ractor::ClosedError. # # r = Ractor.new do # sleep(500) # Ractor.yield 'Hello from ractor' # end # r.close_outgoing # r.take # # Ractor::ClosedError (The outgoing-port is already closed) # # The error would be raised immediately, not when ractor will try to receive # # If an uncaught exception is raised in the Ractor, it is propagated by take as # a Ractor::RemoteError. # # r = Ractor.new {raise "Something weird happened"} # # begin # r.take # rescue => e # p e # => # # p e.ractor == r # => true # p e.cause # => # # end # # Ractor::ClosedError is a descendant of StopIteration, so the termination of # the ractor will break out of any loops that receive this message without # propagating the error: # # r = Ractor.new do # 3.times {|i| Ractor.yield "message #{i}"} # "finishing" # end # # loop {puts "Received: " + r.take} # puts "Continue successfully" # # This will print: # # Received: message 0 # Received: message 1 # Received: message 2 # Received: finishing # Continue successfully # def take: () -> untyped # # alias to_s inspect private # # same as Ractor.receive # def receive: () -> untyped # # same as Ractor.receive_if # def receive_if: () { (untyped) -> boolish } -> untyped # # alias recv receive # # Raised when an attempt is made to send a message to a closed port, or to # retrieve a message from a closed and empty port. Ports may be closed # explicitly with Ractor#close_outgoing/close_incoming and are closed implicitly # when a Ractor terminates. # # r = Ractor.new { sleep(500) } # r.close_outgoing # r.take # Ractor::ClosedError # # ClosedError is a descendant of StopIteration, so the closing of the ractor # will break the loops without propagating the error: # # r = Ractor.new do # loop do # msg = receive # raises ClosedError and loop traps it # puts "Received: #{msg}" # end # puts "loop exited" # end # # 3.times{|i| r << i} # r.close_incoming # r.take # puts "Continue successfully" # # This will print: # # Received: 0 # Received: 1 # Received: 2 # loop exited # Continue successfully # class ClosedError < StopIteration end class Error < RuntimeError end class IsolationError < Ractor::Error end # # Raised on an attempt to access an object which was moved in Ractor#send or # Ractor.yield. # # r = Ractor.new { sleep } # # ary = [1, 2, 3] # r.send(ary, move: true) # ary.inspect # # Ractor::MovedError (can not send any methods to a moved object) # class MovedError < Ractor::Error end # # A special object which replaces any value that was moved to another ractor in # Ractor#send or Ractor.yield. Any attempt to access the object results in # Ractor::MovedError. # # r = Ractor.new { receive } # # ary = [1, 2, 3] # r.send(ary, move: true) # p Ractor::MovedObject === ary # # => true # ary.inspect # # Ractor::MovedError (can not send any methods to a moved object) # class MovedObject < BasicObject public # # def !: (*untyped) -> untyped # # def !=: (*untyped) -> untyped # # def ==: (*untyped) -> untyped # # def __id__: (*untyped) -> untyped # # def __send__: (*untyped) -> untyped # # def equal?: (*untyped) -> untyped # # def instance_eval: (*untyped) -> untyped # # def instance_exec: (*untyped) -> untyped # # def method_missing: (*untyped) -> untyped end # # Raised on attempt to Ractor#take if there was an uncaught exception in the # Ractor. Its `cause` will contain the original exception, and `ractor` is the # original ractor it was raised in. # # r = Ractor.new { raise "Something weird happened" } # # begin # r.take # rescue => e # p e # => # # p e.ractor == r # => true # p e.cause # => # # end # class RemoteError < Ractor::Error public def ractor: () -> Ractor end class UnsafeError < Ractor::Error end %a{annotate:rdoc:skip} class Selector end end