class ForkedProcess

ForkedProcess exposes a small API for performing a block of code in a forked process, and relaying its output to another block.

Public Instance Methods

read(&block) click to toggle source

Public: Define a callback which reads in the output from the forked process.

Yields an IO object opened for reading when ‘run` is invoked. Returns nothing.

# File lib/forked_process.rb, line 22
def read(&block)
  @read_block = block
end
run() click to toggle source

Public: Fork a process, opening a pipe for IO and yielding the write and read components to the relevant blocks.

Returns nothing.

# File lib/forked_process.rb, line 30
def run
  reader, writer = IO.pipe

  pid = fork do
    reader.close
    @write_block.call(writer)
    writer.close
    exit!(0)
  end

  writer.close
  @read_block.call(reader)
  Process.wait(pid)

  raise UnsuccessfulExit unless $CHILD_STATUS.success?
end
write(&block) click to toggle source

Public: Define a callback which will be run in a forked process.

Yields an IO object opened for writing when ‘run` is invoked. Returns nothing.

# File lib/forked_process.rb, line 13
def write(&block)
  @write_block = block
end