D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Ractor
/
Filename :
take-i.ri
back
Copy
U:RDoc::AnyMethod[iI" take:ETI"Ractor#take;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"gGet a message from the ractor's outgoing port, which was put there by Ractor.yield or at ractor's ;TI"termination.;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"r = Ractor.new do ;TI"% Ractor.yield 'explicit yield' ;TI" 'last value' ;TI" end ;TI"&puts r.take #=> 'explicit yield' ;TI""puts r.take #=> 'last value' ;TI"Mputs r.take # Ractor::ClosedError (The outgoing-port is already closed) ;T:@format0o; ; [I"bThe fact that the last value is also sent to the outgoing port means that +take+ can be used ;TI"`as an analog of Thread#join ("just wait until ractor finishes"). However, it will raise if ;TI"0somebody has already consumed that message.;T@o; ; [I"eIf the outgoing port was closed with #close_outgoing, the method will raise Ractor::ClosedError.;T@o;; [ I"r = Ractor.new do ;TI" sleep(500) ;TI"( Ractor.yield 'Hello from ractor' ;TI" end ;TI"r.close_outgoing ;TI"r.take ;TI"A# Ractor::ClosedError (The outgoing-port is already closed) ;TI"R# The error would be raised immediately, not when ractor will try to receive ;T; 0o; ; [I"UIf an uncaught exception is raised in the Ractor, it is propagated by take as a ;TI"Ractor::RemoteError.;T@o;; [I"7r = Ractor.new {raise "Something weird happened"} ;TI" ;TI"begin ;TI" r.take ;TI"rescue => e ;TI"O p e # => #<Ractor::RemoteError: thrown by remote Ractor.> ;TI"" p e.ractor == r # => true ;TI"G p e.cause # => #<RuntimeError: Something weird happened> ;TI" end ;T; 0o; ; [I"gRactor::ClosedError is a descendant of StopIteration, so the termination of the ractor will break ;TI"Nout of any loops that receive this message without propagating the error:;T@o;; [I"r = Ractor.new do ;TI"1 3.times {|i| Ractor.yield "message #{i}"} ;TI" "finishing" ;TI" end ;TI" ;TI"'loop {puts "Received: " + r.take} ;TI""puts "Continue successfully" ;T; 0o; ; [I"This will print:;T@o;; [ I"Received: message 0 ;TI"Received: message 1 ;TI"Received: message 2 ;TI"Received: finishing ;TI"Continue successfully;T; 0: @fileI"ractor.rb;T:0@omit_headings_from_table_of_contents_below0I"ractor.take -> msg ;T0[ I"();T@QFI"Ractor;TcRDoc::NormalClass00