Class: Concurrent::IndirectImmediateExecutor

Inherits:
ImmediateExecutor show all
Defined in:
lib/concurrent/executor/indirect_immediate_executor.rb

Overview

Note:

Intended for use primarily in testing and debugging.

An executor service which runs all operations on a new thread, blocking until it completes. Operations are performed in the order they are received and no two operations can be performed simultaneously.

This executor service exists mainly for testing an debugging. When used it immediately runs every ‘#post` operation on a new thread, blocking the current thread until the operation is complete. This is similar to how the ImmediateExecutor works, but the operation has the full stack of the new thread at its disposal. This can be helpful when the operations will spawn more operations on the same executor and so on - such a situation might overflow the single stack in case of an ImmediateExecutor, which is inconsistent with how it would behave for a threaded executor.

Instance Method Summary collapse

Methods inherited from ImmediateExecutor

#<<, #running?, #shutdown, #shutdown?, #shuttingdown?, #wait_for_termination

Methods included from SerialExecutor

#serialized?

Methods included from Executor

#can_overflow?, #serialized?

Constructor Details

#initializeIndirectImmediateExecutor

Creates a new executor



20
21
22
23
# File 'lib/concurrent/executor/indirect_immediate_executor.rb', line 20

def initialize
  super
  @internal_executor = PerThreadExecutor.new
end

Instance Method Details

#post(*args) { ... } ⇒ Boolean

Submit a task to the executor for asynchronous processing.

Parameters:

  • args (Array)

    zero or more arguments to be passed to the task

Yields:

  • the asynchronous task to perform

Returns:

  • (Boolean)

    ‘true` if the task is queued, `false` if the executor is not running

Raises:

  • (ArgumentError)

    if no task is given



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/concurrent/executor/indirect_immediate_executor.rb', line 26

def post(*args, &task)
  raise ArgumentError.new("no block given") unless block_given?
  return false unless running?

  event = Concurrent::Event.new
  internal_executor.post do
    begin
      task.call(*args)
    ensure
      event.set
    end
  end
  event.wait

  true
end