Class: IProcess
- Inherits:
-
Object
- Object
- IProcess
- Defined in:
- lib/iprocess.rb,
lib/iprocess/version.rb
Constant Summary collapse
- VERSION =
'6.0.0'
Class Method Summary collapse
-
.serializer ⇒ #load, #dump
Returns the serializer used by IProcess.
-
.serializer=(obj) ⇒ Object
-
.spawn(number_of, obj = nil, &worker) ⇒ Object
Spawn one or more subprocesse(s).
Instance Method Summary collapse
-
#execute ⇒ Fixnum
Executes a unit of work in a subprocess.
-
#initialize(worker) ⇒ IProcess
constructor
Returns self.
-
#result ⇒ Object
Returns the return value of the unit of work.
Constructor Details
#initialize(worker) ⇒ IProcess
Returns self.
61 62 63 64 65 66 67 68 69 |
# File 'lib/iprocess.rb', line 61 def initialize(worker) @worker = worker @channel = nil @pid = nil unless @worker.respond_to?(:call) raise ArgumentError, "Expected worker to implement #{@worker.class}#call" end end |
Class Method Details
.serializer ⇒ #load, #dump
Returns the serializer used by IProcess.
9 10 11 |
# File 'lib/iprocess.rb', line 9 def self.serializer @serializer || Marshal end |
.serializer=(obj) ⇒ Object
18 19 20 |
# File 'lib/iprocess.rb', line 18 def self.serializer=(obj) @serializer = obj end |
.spawn(number_of, obj = nil, &worker) ⇒ Object
Spawn one or more subprocesse(s).
@param [Integer] number_of The number of subprocesses to spawn.
@param [#call] worker The unit of work to execute in a subprocess.
@return [Array
@example IProcess.spawn 2 do true end
39 40 41 |
# File 'lib/iprocess.rb', line 39 def self.spawn(number_of, obj = nil, &worker) fork(number_of, obj, &worker).map(&:result) end |
Instance Method Details
#execute ⇒ Fixnum
Executes a unit of work in a subprocess.
77 78 79 80 |
# File 'lib/iprocess.rb', line 77 def execute @channel = IChannel.new IProcess.serializer @pid = fork { @channel.write(@worker.call) } end |
#result ⇒ Object
Returns the return value of the unit of work.
86 87 88 89 90 91 |
# File 'lib/iprocess.rb', line 86 def result Process.wait(@pid) if @channel.readable? @channel.recv end end |