Class: Temporalio::Worker::ThreadPoolExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/worker/thread_pool_executor.rb

Overview

Note:

This is a fixed thread pool that allocated threads eagerly and has an infinite buffer.

A generic fixed thread pool.

This is used to execute multiple activities concurrently and independenty of each other.

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPoolExecutor

Generate new thread pool executor.

Parameters:

  • size (Integer)

    Number of concurrently executing threads.



12
13
14
15
16
17
# File 'lib/temporalio/worker/thread_pool_executor.rb', line 12

def initialize(size)
  @queue = Queue.new
  @pool = Array.new(size) do
    Thread.new { poll }
  end
end

Instance Method Details

#schedule { ... } ⇒ Object

Execute a block of code inside of the threads.

Yields:

  • Block of code to be executed.



22
23
24
# File 'lib/temporalio/worker/thread_pool_executor.rb', line 22

def schedule(&block)
  queue << block
end

#shutdownObject

Stop the thread pool and wait for all threads to finish work.



27
28
29
30
31
32
33
# File 'lib/temporalio/worker/thread_pool_executor.rb', line 27

def shutdown
  pool.size.times do
    schedule { throw EXIT_SYMBOL }
  end

  pool.each(&:join)
end