Class: Tapioca::Executor

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/executor.rb

Constant Summary collapse

MINIMUM_ITEMS_PER_WORKER =
T.let(2, Integer)

Instance Method Summary collapse

Constructor Details

#initialize(queue, number_of_workers: nil) ⇒ Executor

Returns a new instance of Executor.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tapioca/executor.rb', line 11

def initialize(queue, number_of_workers: nil)
  @queue = queue

  # Forking workers is expensive and not worth it for a low number of gems. Here we assign the number of workers to
  # be the minimum between the number of available processors (max) or the number of workers to make sure that each
  # one has at least 4 items to process
  @number_of_workers = T.let(
    number_of_workers || [max_processors, (queue.length.to_f / MINIMUM_ITEMS_PER_WORKER).ceil].min,
    Integer,
  )
end

Instance Method Details

#run_in_parallel(&block) ⇒ Object



28
29
30
31
32
# File 'lib/tapioca/executor.rb', line 28

def run_in_parallel(&block)
  # To have the parallel gem run jobs in the parent process, you must pass 0 as the number of processes
  number_of_processes = @number_of_workers == 1 ? 0 : @number_of_workers
  Parallel.map(@queue, { in_processes: number_of_processes }, &block)
end