Class: ForkedProcess
- Inherits:
-
Object
- Object
- ForkedProcess
- Defined in:
- lib/forked_process.rb
Overview
ForkedProcess exposes a small API for performing a block of code in a forked process, and relaying its output to another block.
Defined Under Namespace
Classes: UnsuccessfulExit
Instance Method Summary collapse
-
#read(&block) ⇒ Object
Public: Define a callback which reads in the output from the forked process.
-
#run ⇒ Object
Public: Fork a process, opening a pipe for IO and yielding the write and read components to the relevant blocks.
-
#write(&block) ⇒ Object
Public: Define a callback which will be run in a forked process.
Instance Method Details
#read(&block) ⇒ Object
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.
22 23 24 |
# File 'lib/forked_process.rb', line 22 def read(&block) @read_block = block end |
#run ⇒ Object
Public: Fork a process, opening a pipe for IO and yielding the write and read components to the relevant blocks.
Returns nothing.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
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.
13 14 15 |
# File 'lib/forked_process.rb', line 13 def write(&block) @write_block = block end |