Class: Ractor::Dispatch::Executor
- Inherits:
-
Object
- Object
- Ractor::Dispatch::Executor
- Defined in:
- lib/ractor/dispatch/executor.rb
Instance Method Summary collapse
-
#initialize ⇒ Executor
constructor
A new instance of Executor.
- #run(&block) ⇒ Object
- #shutdown ⇒ Object
- #submit(&block) ⇒ Object
Constructor Details
#initialize ⇒ Executor
Returns a new instance of Executor.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ractor/dispatch/executor.rb', line 8 def initialize @port = begin Ractor::Port.new rescue NameError raise Unsupported, "Ractor-dispatch requires Ractor::Port (Ruby 4.0+)" end Thread.new do loop do callable, reply_port = @port.receive begin result = callable.call reply_port << [:ok, result] rescue => e reply_port << [:error, e] rescue Ractor::ClosedError # caller went away, discard end rescue Ractor::Error => e reply_port << [:error, Ractor::Dispatch::Error.new("Executor encountered a problem", details: serialize_error(e))] end rescue Ractor::ClosedError # port closed via shutdown end Ractor.make_shareable(self) end |
Instance Method Details
#run(&block) ⇒ Object
44 45 46 |
# File 'lib/ractor/dispatch/executor.rb', line 44 def run(&block) submit(&block).value end |
#shutdown ⇒ Object
48 49 50 |
# File 'lib/ractor/dispatch/executor.rb', line 48 def shutdown @port.close end |