Class: Braintrust::Internal::ThreadPool::Collect

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

Overview

Strategy for collecting results in input order

Instance Method Summary collapse

Instance Method Details

#empty_resultObject



85
86
87
# File 'lib/braintrust/internal/thread_pool.rb', line 85

def empty_result
  []
end

#enqueue_sentinel(count) ⇒ Object



68
69
70
# File 'lib/braintrust/internal/thread_pool.rb', line 68

def enqueue_sentinel(count)
  count.times { @queue << :done }
end

#prepare(items) ⇒ Object



62
63
64
65
66
# File 'lib/braintrust/internal/thread_pool.rb', line 62

def prepare(items)
  @results = Array.new(items.size)
  @queue = Queue.new
  items.each_with_index { |item, idx| @queue << [item, idx] }
end

#resultObject



81
82
83
# File 'lib/braintrust/internal/thread_pool.rb', line 81

def result
  @results
end

#sequential_run(items, &block) ⇒ Object



89
90
91
# File 'lib/braintrust/internal/thread_pool.rb', line 89

def sequential_run(items, &block)
  items.map(&block)
end

#work_loop(&block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/braintrust/internal/thread_pool.rb', line 72

def work_loop(&block)
  loop do
    work = @queue.pop
    break if work == :done
    item, idx = work
    @results[idx] = block.call(item)
  end
end