instance method
Ractor::Port#receive
() -> T
port.receive → msg
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ractor.rb, line 741
def receive
__builtin_cexpr! %q{
ractor_port_receive(ec, self)
}
end
Receives a message from the port (which was sent there by Port#send). Only the ractor that created the port can receive messages this way.
Example
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.
Example
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 Ractor::ClosedError.
Example
port = Ractor::Port.new port.close port.receive #=> raise Ractor::ClosedError