Class: Aw::Fork Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The Fork class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read, write) ⇒ Fork

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the class.

Parameters:

  • read (IO)

    The read endpoint.

  • write (IO)

    The write endpoint.

Raises:

  • (::NotImplementedError)


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

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

  @read   = read
  @write  = write
end

Instance Attribute Details

#readIO (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The read endpoint.

Returns:

  • (IO)

    The read endpoint.



25
26
27
# File 'lib/aw/fork.rb', line 25

def read
  @read
end

#writeIO (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The write endpoint.

Returns:

  • (IO)

    The write endpoint.



30
31
32
# File 'lib/aw/fork.rb', line 30

def write
  @write
end

Instance Method Details

#call#object_id

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the block inside a sub-process, and returns the computed value.

Examples:

Computes ‘6 * 7` in a sub-process and returns `42` to the current process.

call { 6 * 7 } # => 42

Returns:

  • (#object_id)

    Returns the value that has been returned in the block.

Raises:

  • (Exception)

    Exceptions raised in a block of code are propagated.



39
40
41
42
43
44
45
46
47
48
# File 'lib/aw/fork.rb', line 39

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

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