Class: BioDSL::Fork

Inherits:
Object
  • Object
show all
Defined in:
lib/BioDSL/fork.rb

Overview

Class containing methods to fork in an objective oriented manner.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Fork

Constructor for Fork.

Parameters:

  • block (Proc)

    Block to execute.

Raises:

  • (ArgumentError)

    If no block given.



54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



35
36
37
# File 'lib/BioDSL/fork.rb', line 35

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



35
36
37
# File 'lib/BioDSL/fork.rb', line 35

def output
  @output
end

Class Method Details

.execute(&block) ⇒ Fork

Class method to execute a block in a seperate process.

Parameters:

  • block (Proc)

    Block to execute.

Returns:

  • (Fork)

    Instance of Fork.



42
43
44
45
# File 'lib/BioDSL/fork.rb', line 42

def self.execute(&block)
  parent = new(&block)
  parent.execute
end

Instance Method Details

#executeObject

Execute the block in a separate process.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#readObject

Read object from forked process.

Raises:



94
95
96
97
98
# File 'lib/BioDSL/fork.rb', line 94

def read
  fail BioDSL::ForkError, 'Not running' unless running?

  @input.read
end

#running?Bool

Determines if process is running.

Returns:

  • (Bool)

    True if running else nil.



87
88
89
# File 'lib/BioDSL/fork.rb', line 87

def running?
  @pid
end

#waitObject

Wait for forked process.

Raises:



112
113
114
115
116
117
118
119
# 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(obj) ⇒ Object

Write object to forked process.

Raises:



103
104
105
106
107
# File 'lib/BioDSL/fork.rb', line 103

def write(obj)
  fail BioDSL::ForkError, 'Not running' unless running?

  @output.write(obj)
end