Class: Ractor::Dispatch::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor/dispatch/executor.rb

Instance Method Summary collapse

Constructor Details

#initializeExecutor

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

#shutdownObject



48
49
50
# File 'lib/ractor/dispatch/executor.rb', line 48

def shutdown
  @port.close
end

#submit(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/ractor/dispatch/executor.rb', line 37

def submit(&block)
  callable = Ractor.shareable_proc(&block)
  reply_port = Ractor::Port.new
  @port << [callable, reply_port]
  Future.new(reply_port)
end