Class: Chingu::AsyncTasks::Parallel

Inherits:
Chingu::Async::BasicTask show all
Defined in:
lib/chingu/async_tasks/parallel.rb

Overview

Executes all subtasks in parallel.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Parallel

Returns a new instance of Parallel



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chingu/async_tasks/parallel.rb', line 30

def initialize(&block)
  @subtasks = []
  
  # Make @subtasks behave like a TaskList for the TaskBuilder.
  # This is probably a dirty hack!
  class <<@subtasks
    alias :enq   :push
    alias :deq   :shift
    alias :front :first
  end
  
  add_tasks(&block)
end

Instance Method Details

#add_tasks(&block) ⇒ Object



44
45
46
47
# File 'lib/chingu/async_tasks/parallel.rb', line 44

def add_tasks(&block)
  builder = Chingu::Async::TaskBuilder.new(@subtasks)
  block[builder]
end

#finished?Boolean

Returns true if all subtasks have finished executing.

Returns:

  • (Boolean)


52
53
54
# File 'lib/chingu/async_tasks/parallel.rb', line 52

def finished?
  @subtasks.empty? or @subtasks.all?(&:finished?)
end

#update(object) ⇒ Object



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

def update(object)
  @subtasks.each { |task| task.update(object) }
end