class BioDSL::Fork
Class containing methods to fork in an objective oriented manner.
Attributes
Public Class Methods
Class method to execute a block in a seperate process.
@param block [Proc] Block to execute.
@return [Fork] Instance of Fork
.
# File lib/BioDSL/fork.rb, line 42 def self.execute(&block) parent = new(&block) parent.execute end
Constructor for Fork
.
@param block [Proc] Block to execute.
@raise [ArgumentError] If no block given.
@return [Fork] Instance of Fork
.
# File lib/BioDSL/fork.rb, line 54 def initialize(&block) fail ArgumentError, 'No block given' unless block @parent = true @alive = false @pid = nil @input = nil @output = nil @block = block end
Public Instance Methods
Execute the block in a separate process.
# File lib/BioDSL/fork.rb, line 66 def execute @alive = true child_read, parent_write = BioDSL::Stream.pipe parent_read, child_write = BioDSL::Stream.pipe pid = fork_process(child_read, child_write, parent_read, parent_write) child_write.close child_read.close @pid = pid @input = parent_read @output = parent_write self end
Read object from forked process.
@raise [ForkError] unless process is running.
# File lib/BioDSL/fork.rb, line 94 def read fail BioDSL::ForkError, 'Not running' unless running? @input.read end
Determines if process is running.
@return [Bool] True if running else nil.
# File lib/BioDSL/fork.rb, line 87 def running? @pid end
Wait for forked process.
@raise [ForkError] unless process is running.
# File lib/BioDSL/fork.rb, line 112 def wait fail BioDSL::ForkError, 'Not running' unless running? @input.close unless @input.closed? @output.close unless @output.closed? Process.wait(@pid) end
Write object to forked process.
@raise [ForkError] unless process is running.
# File lib/BioDSL/fork.rb, line 103 def write(obj) fail BioDSL::ForkError, 'Not running' unless running? @output.write(obj) end
Private Instance Methods
Fork
process with IPC.
@param child_read [BioDSL::Stream] Child read IO. @param child_write [BioDSL::Stream] Child write IO. @param parent_read [BioDSL::Stream] Parent read IO. @param parent_write [BioDSL::Stream] Parent write IO.
@return [FixNum] Process ID.
# File lib/BioDSL/fork.rb, line 131 def fork_process(child_read, child_write, parent_read, parent_write) Process.fork do parent_write.close parent_read.close @parent = false @pid = Process.pid @input = child_read @output = child_write @block.call(self) end end