Module: CMDx::Executors::Thread Private

Extended by:
Thread
Included in:
Thread
Defined in:
lib/cmdx/executors/thread.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Default executor. Uses a fixed-size ‘Thread` pool drained via a `Queue`; sentinel `nil`s terminate workers. Workers inherit the parent’s chain via fiber-local storage.

Instance Method Summary collapse

Instance Method Details

#call(jobs:, concurrency:, on_job:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • jobs (Array)

    opaque job objects forwarded to ‘on_job`

  • concurrency (Integer)

    worker count

  • on_job (#call)

    unary callable invoked per job



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cmdx/executors/thread.rb', line 18

def call(jobs:, concurrency:, on_job:)
  queue = Queue.new
  jobs.each { |job| queue << job }
  concurrency.times { queue << nil }

  workers = Array.new(concurrency) do
    ::Thread.new do
      while (job = queue.pop)
        on_job.call(job)
      end
    end
  end

  workers.each(&:join)
end