class Ractor::Port
Public Class Methods
Source
static VALUE ractor_port_initialzie(VALUE self) { return ractor_port_init(self, GET_RACTOR()); }
Public Instance Methods
Source
# File ractor.rb, line 742 def close __builtin_cexpr! %q{ ractor_port_close(ec, self) } end
Close the port. On the closed port, sending is not prohibited. Receiving is also not allowed if there is no sent messages arrived before closing.
port = Ractor::Port.new Ractor.new port do |port| port.send 1 # OK port.send 2 # OK port.close port.send 3 # raise Ractor::ClosedError end port.receive #=> 1 port.receive #=> 2 port.receive #=> raise Ractor::ClosedError
Now, only a Ractor
which creates the port is allowed to close ports.
port = Ractor::Port.new Ractor.new port do |port| port.close #=> closing port by other ractors is not allowed (Ractor::Error) end.join
Source
# File ractor.rb, line 753 def closed? __builtin_cexpr! %q{ ractor_port_closed_p(ec, self); } end
Return the port is closed or not.
Source
static VALUE ractor_port_initialzie_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; dst->id_ = ractor_port_id(src); return self; }
Source
# File ractor.rb, line 762 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
Source
# File ractor.rb, line 665 def receive __builtin_cexpr! %q{ ractor_port_receive(ec, self) } end
Receive a message to the port (which was sent there by Port#send
).
port = Ractor::Port.new r = Ractor.new port do |port| port.send('message1') end v1 = port.receive puts "Received: #{v1}" r.join # Here will be printed: "Received: message1"
The method blocks 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 close_incoming was called on the ractor, the method raises Ractor::ClosedError
if there are no more messages in the message queue:
port = Ractor::Port.new port.close port.receive #=> raise Ractor::ClosedError
Source
# File ractor.rb, line 708 def send obj, move: false __builtin_cexpr! %q{ ractor_port_send(ec, self, obj, move) } end
Send a message to a port to be accepted by port.receive.
port = Ractor::Port.new r = Ractor.new do r.send 'message' end value = port.receive puts "Received #{value}" # Prints: "Received: message"
The method is non-blocking (will return immediately even if the ractor 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 port which already closed its execution will raise Ractor::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, it only send a reference to the object without cloning.