Class: SizedParallel::Task

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

Overview

This is just a Struct with no actual methods but #then.

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Task

Returns a new instance of Task.

Parameters:

  • pool (Pool)

    a pool.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sized_parallel/task.rb', line 30

def initialize pool
  # (Surprisingly  enough) this  class  does _not_  hold  the task  procedure
  # itself.  Instead  the environment that  is enclosed into a  closure holds
  # this instance, and that does the job.  This seems tricky, but a task is a
  # closure by  nature so a closure  holds a task  and not vice versa  is the
  # right way.
  raise ArgumentError, 'no block given' unless defined? yield
  @p = pool
  @q = Queue.new
  pool.process do
    begin
      (*val) = yield self
    ensure
      @q.enq val
    end
  end
end

Instance Method Details

#then {|*argv| ... } ⇒ Task

Creates another Task, that "depends" self. The returned task waits until self finishes.

Yield Parameters:

  • *argv (...)

    the return value of previous task.

Yield Returns:

  • (...)

    passed to the next task.

Returns:

  • (Task)

    a new task that does the given block.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'lib/sized_parallel/task.rb', line 53

def then
  raise ArgumentError, 'no block given' unless defined? yield
  Thread.pass
  self.class.new @p do
    v = @q.deq
    next yield(*v)
  end
end