Method: Fork.future

Defined in:
lib/fork.rb

.future(*args) ⇒ Proc

A simple forked-future implementation. Will process the block in a fork, blocks upon request of the result until the result is present. If the forked code raises an exception, invoking call on the proc will raise that exception in the parent process.

Examples:

Usage

# A
Fork.future { 1 }.call # => 1

# B
result = Fork.future { sleep 2; 1 } # assume a complex computation instead of sleep(2)
sleep 2                             # assume another complex computation
start  = Time.now
result.call                         # => 1
elapsed_time = Time.now-start       # => <1s as the work was done parallely

Parameters:

  • args

    All parameters passed to Fork.future are passed on to the block.

Returns:

  • (Proc)

    A lambda which upon invoking #call will block until the result of the block is calculated.



118
119
120
121
122
123
124
# File 'lib/fork.rb', line 118

def self.future(*args)
  obj = execute :return => true do |parent|
    yield(*args)
  end

  lambda { obj.return_value }
end