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.



58
59
60
61
62
# File 'lib/parallel.rb', line 58

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

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



55
56
57
# File 'lib/parallel.rb', line 55

def pid
  @pid
end

#readObject (readonly)

Returns the value of attribute read.



55
56
57
# File 'lib/parallel.rb', line 55

def read
  @read
end

#threadObject

Returns the value of attribute thread.



56
57
58
# File 'lib/parallel.rb', line 56

def thread
  @thread
end

#writeObject (readonly)

Returns the value of attribute write.



55
56
57
# File 'lib/parallel.rb', line 55

def write
  @write
end

Instance Method Details

#close_pipesObject

might be passed to started_processes and simultaneously closed by another thread when running in isolation mode, so we have to check if it is closed before closing



71
72
73
74
# File 'lib/parallel.rb', line 71

def close_pipes
  read.close unless read.closed?
  write.close unless write.closed?
end

#stopObject



64
65
66
67
# File 'lib/parallel.rb', line 64

def stop
  close_pipes
  wait # if it goes zombie, rather wait here to be able to debug
end

#work(data) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/parallel.rb', line 76

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

  result = begin
    Marshal.load(read)
  rescue EOFError
    raise DeadWorker
  end
  raise result.exception if result.is_a?(ExceptionWrapper)
  result
end