Class: Aw::Fork

Inherits:
Object
  • Object
show all
Defined in:
lib/aw/fork.rb

Overview

The Fork class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read, write) ⇒ Fork

Initialize the class.

Parameters:

  • read (IO)

    The read endpoint.

  • write (IO)

    The write endpoint.



12
13
14
15
16
17
18
# File 'lib/aw/fork.rb', line 12

def initialize(read, write)
  # Currently, not available on all platforms.
  raise 'fork() unimplemented' unless ::Process.respond_to?(:fork)

  @read   = read
  @write  = write
end

Instance Attribute Details

#readIO (readonly)

Returns The read endpoint.

Returns:

  • (IO)

    The read endpoint.



23
24
25
# File 'lib/aw/fork.rb', line 23

def read
  @read
end

#writeIO (readonly)

Returns The write endpoint.

Returns:

  • (IO)

    The write endpoint.



28
29
30
# File 'lib/aw/fork.rb', line 28

def write
  @write
end

Instance Method Details

#call(&block) ⇒ #object_id

Run the block inside a subprocess, and return the value.

Returns:

  • (#object_id)

    The result.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aw/fork.rb', line 33

def call(*, **, &block)
  pid = fork_and_return_pid(&block)
  write.close
  result = read.read
  ::Process.wait(pid)

  # rubocop:disable MarshalLoad
  ::Marshal.load(result).tap do |r|
    raise r if r.is_a?(::Exception)
  end
  # rubocop:enable MarshalLoad
end