Module: Aw

Defined in:
lib/aw.rb,
lib/aw/fork.rb

Overview

Namespace for the Aw library.

Examples:

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

Aw.fork! { 6 * 7 } # => 42

Computes ‘6 * 7` in a sub-process and returns `true` to the current process if no exception is thrown.

Aw.fork? { 6 * 7 } # => true

Defined Under Namespace

Classes: Fork

Class Method Summary collapse

Class Method Details

.fork!#object_id

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.

Aw.fork! { 6 * 7 } # => 42

Computes ‘nil + 1` in a sub-process and raises `NoMethodError` to the current process.

Aw.fork! { nil + 1 } # => raise NoMethodError (undefined method `+' for nil:NilClass)

Returns:

  • (#object_id)

    Returns the value that has been returned in the block.

Raises:

  • (Exception)

    Exceptions raised in a block of code are propagated.



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

def self.fork!(&)
  read, write = ::IO.pipe
  Fork.new(read, write).call(&)
end

.fork?Boolean

Runs the block inside a sub-process, and returns ‘true` if no exception is thrown. Otherwise when an exception is raised, `false` is returned.

Examples:

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

Aw.fork? { 6 * 7 } # => true

Computes ‘nil + 1` in a sub-process and returns `false` to the current process.

Aw.fork? { nil + 1 } # => false

Returns:

  • (Boolean)

    Returns ‘true` if stat is successful, `false` if not.



38
39
40
41
42
# File 'lib/aw.rb', line 38

def self.fork?(&)
  pid = ::Process.fork(&)
  _, status = ::Process.wait2(pid)
  status.success?
end