123456789_123456789_123456789_123456789_123456789_

Class: Ractor::Port

Relationships & Source Files
Inherits: Object
Defined in: ractor.rb,
ractor_sync.c

Overview

Port objects transmit messages between Ractors.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newPort

Returns a new Port object.

[ GitHub ]

  
# File 'ractor_sync.c', line 101

static VALUE
ractor_port_initialize(VALUE self)
{
    return ractor_port_init(self, GET_RACTOR());
}

Instance Attribute Details

#closed?Boolean (readonly)

Returns whether or not the port is closed.

[ GitHub ]

  
# File 'ractor.rb', line 817

def closed?
  __builtin_cexpr! %q{
    ractor_port_closed_p(ec, self);
  }
end

Instance Method Details

#<<(obj, move: false)

Alias for #send.

[ GitHub ]

  
# File 'ractor.rb', line 790

alias << send

#close

Closes the port. Sending to a closed port is prohibited. Receiving is also prohibited if there are no messages in its message queue.

Only the ::Ractor which created the port is allowed to close it.

port = Ractor::Port.new
Ractor.new port do |port|
  port.close #=> closing port by other ractors is not allowed (Ractor::Error)
end.join
[ GitHub ]

  
# File 'ractor.rb', line 806

def close
  __builtin_cexpr! %q{
    ractor_port_close(ec, self)
  }
end

#initialize_copy(orig)

This method is for internal use only.
[ GitHub ]

  
# File 'ractor_sync.c', line 108

static VALUE
ractor_port_initialize_copy(VALUE self, VALUE orig)
{
    struct ractor_port *dst = RACTOR_PORT_PTR(self);
    struct ractor_port *src = RACTOR_PORT_PTR(orig);
    dst->r = src->r;
    RB_OBJ_WRITTEN(self, Qundef, dst->r->pub.self);
    dst->id_ = ractor_port_id(src);

    return self;
}

#inspectString

[ GitHub ]

  
# File 'ractor.rb', line 826

def inspect
  "#<Ractor::Port to:\##{
    __builtin_cexpr! "SIZET2NUM(rb_ractor_id((RACTOR_PORT_PTR(self)->r)))"
  } id:#{
    __builtin_cexpr! "SIZET2NUM(ractor_port_id(RACTOR_PORT_PTR(self)))"
  }>"
end

#receivemsg

Receives a message from the port (which was sent there by #send). Only the ractor that created the port can receive messages this way.

port = Ractor::Port.new
r = Ractor.new port do |port|
  port.send('message1')
end

v1 = port.receive
puts "Received: #{v1}"
r.join
# This will print: "Received: message1"

The method blocks the current ::Thread if the message queue is empty.

port = Ractor::Port.new
r = Ractor.new port do |port|
  wait
  puts "Still not received"
  port.send('message1')
  wait
  puts "Still received only one"
  port.send('message2')
end
puts "Before first receive"
v1 = port.receive
puts "Received: #{v1}"
v2 = port.receive
puts "Received: #{v2}"
r.join

Output:

Before first receive
Still not received
Received: message1
Still received only one
Received: message2

If the port is closed and there are no more messages in the message queue, the method raises ClosedError.

port = Ractor::Port.new
port.close
port.receive #=> raise Ractor::ClosedError
[ GitHub ]

  
# File 'ractor.rb', line 741

def receive
  __builtin_cexpr! %q{
    ractor_port_receive(ec, self)
  }
end

#send(msg, move: false) ⇒ self Also known as: #<<

Sends a message to the port to be accepted by port.receive.

port = Ractor::Port.new
r = Ractor.new(port) do |port|
  port.send 'message'
end
value = port.receive
puts "Received #{value}"
# Prints: "Received: message"

The method is non-blocking (it will return immediately even if the ractor that created the port is not ready to receive anything):

port = Ractor::Port.new
r = Ractor.new(port) do |port|
  port.send 'test'
  puts "Sent successfully"
  # Prints: "Sent successfully" immediately
end

An attempt to send to a closed port will raise ClosedError.

r = Ractor.new {Ractor::Port.new}
r.join
p r
# "#<Ractor:#6 (irb):23 terminated>"
port = r.value
port.send('test') # raise Ractor::ClosedError

If the obj is unshareable, by default it will be copied into the receiving ractor by deep cloning.

If the object is shareable, a reference to the object will be sent to the receiving ractor.

[ GitHub ]

  
# File 'ractor.rb', line 784

def send obj, move: false
  __builtin_cexpr! %q{
    ractor_port_send(ec, self, obj, move)
  }
end