Class: Concurrent::ImmediateExecutor

Inherits:
AbstractExecutorService
  • Object
show all
Defined in:
lib/concurrent/executor/immediate_executor.rb

Overview

Note:

Intended for use primarily in testing and debugging.

An executor service which runs all operations on the current thread, blocking as necessary. 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 the current thread, blocking that thread until the operation is complete. This can be very beneficial during testing because it makes all operations deterministic.

Direct Known Subclasses

IndirectImmediateExecutor

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ImmediateExecutor) initialize

Creates a new executor



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

def initialize
  @stopped = Concurrent::Event.new
end

Instance Attribute Details

- (Symbol) fallback_policy (readonly) Originally defined in class AbstractExecutorService

Returns The fallback policy in effect. Either :abort, :discard, or :caller_runs.

Returns:

  • (Symbol)

    The fallback policy in effect. Either :abort, :discard, or :caller_runs.

Instance Method Details

- (self) <<(task)

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



34
35
36
37
# File 'lib/concurrent/executor/immediate_executor.rb', line 34

def <<(task)
  post(&task)
  self
end

- (Boolean) can_overflow? Originally defined in module ExecutorService

Note:

Always returns false

Does the task queue have a maximum size?

Returns:

  • (Boolean)

    True if the task queue has a maximum size else false.

- (undocumented) log(level, progname, message = nil, &block) Originally defined in module Concern::Logging

Logs through Concurrent.global_logger, it can be overridden by setting @logger

Parameters:

  • level (Integer)

    one of Logger::Severity constants

  • progname (String)

    e.g. a path of an Actor

  • message (String, nil) (defaults to: nil)

    when nil block is used to generate the message

Yield Returns:

  • (String)

    a message

- (Boolean) post(*args) { ... }

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
# File 'lib/concurrent/executor/immediate_executor.rb', line 26

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

- (Boolean) running?

Is the executor running?

Returns:

  • (Boolean)

    true when running, false when shutting down or shutdown



40
41
42
# File 'lib/concurrent/executor/immediate_executor.rb', line 40

def running?
  ! shutdown?
end

- (Boolean) serialized? Originally defined in module SerialExecutorService

Note:

Always returns true

Does this executor guarantee serialization of its operations?

Returns:

  • (Boolean)

    True if the executor guarantees that all operations will be post in the order they are received and no two operations may occur simultaneously. Else false.

- (undocumented) shutdown Also known as: kill

Begin an orderly shutdown. Tasks already in the queue will be executed, but no new tasks will be accepted. Has no additional effect if the thread pool is not running.



55
56
57
58
# File 'lib/concurrent/executor/immediate_executor.rb', line 55

def shutdown
  @stopped.set
  true
end

- (Boolean) shutdown?

Is the executor shutdown?

Returns:

  • (Boolean)

    true when shutdown, false when shutting down or running



50
51
52
# File 'lib/concurrent/executor/immediate_executor.rb', line 50

def shutdown?
  @stopped.set?
end

- (Boolean) shuttingdown?

Is the executor shuttingdown?

Returns:

  • (Boolean)

    true when not running and not shutdown, else false



45
46
47
# File 'lib/concurrent/executor/immediate_executor.rb', line 45

def shuttingdown?
  false
end

- (Boolean) wait_for_termination(timeout = nil)

Note:

Does not initiate shutdown or termination. Either shutdown or kill must be called before this method (or on another thread).

Block until executor shutdown is complete or until timeout seconds have passed.

Parameters:

  • timeout (Integer) (defaults to: nil)

    the maximum number of seconds to wait for shutdown to complete

Returns:

  • (Boolean)

    true if shutdown complete or false on timeout



62
63
64
# File 'lib/concurrent/executor/immediate_executor.rb', line 62

def wait_for_termination(timeout = nil)
  @stopped.wait(timeout)
end