Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Ractor#monitor
# File tmp/rubies/ruby-4.1.0-preview1/ractor.rb, line 673
def monitor port
__builtin_ractor_monitor(port)
end
Registers the port as a monitoring port for this ractor. When the ractor terminates, the port receives an Array naming the ractor and what happened to it, so that several ractors can report to one port.
-
[ractor, :exited]if the ractor terminated without an unhandled exception. -
[ractor, :aborted]if it terminated by one.
The Array is built for the receiving ractor, so watching many ractors does not leave shareable objects behind.
Returns true if the monitor was registered (the ractor is still running). Returns false if the ractor had already terminated; in that case the termination message is sent to the port immediately.
r = Ractor.new{ some_task() } r.monitor(port = Ractor::Port.new) port.receive #=> [r, :exited] r = Ractor.new{ raise "foo" } r.monitor(port = Ractor::Port.new) port.receive #=> [r, :aborted]
Ruby version changes
-
4.0
-
Ractor#monitorandRactor#unmonitorwere added as low-level interfaces used internally to implementRactor#join.
-