Class: Enparallel::ThreadPool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks, requested_worker_count, rule) ⇒ ThreadPool

Returns a new instance of ThreadPool.



5
6
7
8
9
# File 'lib/enparallel/thread_pool.rb', line 5

def initialize(tasks, requested_worker_count, rule)
    @tasks = tasks
    @worker_count = [tasks.length, requested_worker_count].min
    @picker = Picker.new(tasks, rule.to_sym)
end

Instance Attribute Details

#worker_countObject

Returns the value of attribute worker_count.



3
4
5
# File 'lib/enparallel/thread_pool.rb', line 3

def worker_count
  @worker_count
end

Instance Method Details

#drainObject



16
17
18
19
20
21
22
23
24
# File 'lib/enparallel/thread_pool.rb', line 16

def drain
    @workers = @worker_count.times.map do
        Thread.new do
            while (task = @picker.next)
                task.run
            end
        end
    end
end

#drain_waitObject



11
12
13
14
# File 'lib/enparallel/thread_pool.rb', line 11

def drain_wait
    drain
    join
end

#failed_tasksObject



38
39
40
# File 'lib/enparallel/thread_pool.rb', line 38

def failed_tasks
    @tasks.reject(&:succeeded?)
end

#get_log_groupsObject



51
52
53
# File 'lib/enparallel/thread_pool.rb', line 51

def get_log_groups
    logger.get_log_groups
end

#loggerObject



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

def logger
    @logger ||= Logger.from_thread_pool(self)
end

#renderObject



30
31
32
# File 'lib/enparallel/thread_pool.rb', line 30

def render
    @tasks.map(&:char).join.bold
end

#sizeObject



26
27
28
# File 'lib/enparallel/thread_pool.rb', line 26

def size
    @tasks.length
end

#succeeded_tasksObject



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

def succeeded_tasks
    @tasks.select(&:succeeded?)
end

#tasks_of(type) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/enparallel/thread_pool.rb', line 42

def tasks_of(type)
    case type
    when :success
        succeeded_tasks
    when :failure
        failed_tasks
    end
end