Class: Parallel::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read, write, pid) ⇒ Worker

Returns a new instance of Worker.



36
37
38
# File 'lib/parallel.rb', line 36

def initialize(read, write, pid)
  @read, @write, @pid = read, write, pid
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



34
35
36
# File 'lib/parallel.rb', line 34

def pid
  @pid
end

#readObject (readonly)

Returns the value of attribute read.



34
35
36
# File 'lib/parallel.rb', line 34

def read
  @read
end

#threadObject

Returns the value of attribute thread.



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

def thread
  @thread
end

#writeObject (readonly)

Returns the value of attribute write.



34
35
36
# File 'lib/parallel.rb', line 34

def write
  @write
end

Instance Method Details

#close_pipesObject



40
41
42
43
# File 'lib/parallel.rb', line 40

def close_pipes
  read.close
  write.close
end

#waitObject



45
46
47
48
49
# File 'lib/parallel.rb', line 45

def wait
  Process.wait(pid)
rescue Interrupt
  # process died
end

#work(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/parallel.rb', line 51

def work(data)
  begin
    Marshal.dump(data, write)
  rescue Errno::EPIPE
    raise DeadWorker
  end

  begin
    Marshal.load(read)
  rescue EOFError
    raise DeadWorker
  end
end