Class Spork::Forker
In: lib/spork/forker.rb
Parent: Object

A helper class that allows you to run a block inside of a fork, and then get the result from that block.

Example:

  forker = Spork::Forker.new do
    sleep 3
    "success"
  end

  forker.result # => "success"

Methods

abort   new   result   running?  

Classes and Modules

Class Spork::Forker::ForkDiedException

Public Class methods

[Source]

# File lib/spork/forker.rb, line 15
  def initialize(&block)
    return unless block_given?
    @child_io, @server_io = UNIXSocket.socketpair
    @child_pid = Kernel.fork do
      @server_io.close
      Marshal.dump(yield, @child_io)
      # wait for the parent to acknowledge receipt of the result.
      master_response = 
        begin
          Marshal.load(@child_io)
        rescue EOFError
          nil
        end
      
      # terminate, skipping any at_exit blocks.
      exit!(0)
    end
    @child_io.close
  end

Public Instance methods

abort the current running fork

[Source]

# File lib/spork/forker.rb, line 55
  def abort
    if running?
      Process.kill(Signal.list['TERM'], @child_pid)
      @child_pid = nil
      true
    end
  end

Wait for the fork to finish running, and then return its return value.

If the fork was aborted, then result returns nil.

[Source]

# File lib/spork/forker.rb, line 38
  def result
    return unless running?
    result_thread = Thread.new do
      begin
        @result = Marshal.load(@server_io)
        Marshal.dump('ACK', @server_io)
      rescue ForkDiedException
        @result = nil
      end
    end
    Process.wait(@child_pid)
    result_thread.raise(ForkDiedException) if @result.nil?
    @child_pid = nil
    @result
  end

[Source]

# File lib/spork/forker.rb, line 63
  def running?
    return false unless @child_pid
    Process.getpgid(@child_pid)
    true
  rescue Errno::ESRCH
    false
  end

[Validate]