module Aw

Namespace for the Aw library.

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

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

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

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

@api public

Public Class Methods

fork!(&block) click to toggle source

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

@param block [Proc] The code to run in a sub-process.

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

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

@example 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)

@raise [Exception] Exceptions raised in a block of code are propagated. @return [#object_id] Returns the value that has been returned in the block.

# File lib/aw.rb, line 25
def self.fork!(&block)
  read, write = ::IO.pipe
  Fork.new(read, write).call(&block)
end
fork?(&block) click to toggle source

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

@param block [Proc] The code to run in a sub-process.

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

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

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

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

@return [Boolean] Returns ‘true` if stat is successful, `false` if not.

# File lib/aw.rb, line 42
def self.fork?(&block)
  pid = ::Process.fork(&block)
  _, status = ::Process.wait2(pid)
  status.success?
end