Class: Concurrent::WrappingExecutor — Concurrent Ruby

# Class: Concurrent::WrappingExecutor

Inherits:
  Synchronization::Object
  - Object
  - Synchronization::Object
  - Concurrent::WrappingExecutor
  show all

Defined in:
  lib/concurrent-ruby-edge/concurrent/executor/wrapping_executor.rb

## Overview

A delegating executor which modifies each task with arguments before the task is given to the target executor it delegates to.

Examples:

Count task executions

    counter          = AtomicFixnum.new
    count_executions = WrappingExecutor.new Concurrent.global_io_executor do |*args, &task|
      [*args, -> *args { counter.increment; task.call *args }]
    end
    10.times { count_executions.post { :do_something } }
    sleep 0.01
    counter.value #=> 10

## Instance Method Summary collapse

- #can_overflow? ⇒ Boolean

  Does the task queue have a maximum size?.

- #initialize(executor) {|*args, &task| ... } ⇒ WrappingExecutor constructor

  A new instance of WrappingExecutor.

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

  Submit a task to the executor for asynchronous processing.

- #serialized? ⇒ Boolean

  Does this executor guarantee serialization of its operations?.

## Constructor Details

### #initialize(executor) {|*args, &task| ... } ⇒ `WrappingExecutor`

Returns a new instance of WrappingExecutor.

Parameters:

- executor (`Executor`) —

  an executor to delegate the tasks to

Yields:

- (`*args`, `&task`) —

  A function which can modify the task with arguments

Yield Parameters:

- *args (`Array<Object>`) —

  the arguments submitted with the tasks

- &task (`block`) —

  the task submitted to the executor to be modified

Yield Returns:

- (`Array<Object>`) —

  a new arguments and task `[*args, task]` which are submitted to the target executor

    23
    24
    25
    26
    27
    # File 'lib/concurrent-ruby-edge/concurrent/executor/wrapping_executor.rb', line 23

    def initialize(executor, &wrapper)
      super()
      @Wrapper  = wrapper
      @Executor = executor
    end

## Instance Method Details

### #can_overflow? ⇒ `Boolean`

Does the task queue have a maximum size?

Returns:

- (`Boolean`) —

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

    38
    39
    40
    # File 'lib/concurrent-ruby-edge/concurrent/executor/wrapping_executor.rb', line 38

    def can_overflow?
      @Executor.can_overflow?
    end

### #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

See Also:

- how the tasks can be modified

    32
    33
    34
    35
    # File 'lib/concurrent-ruby-edge/concurrent/executor/wrapping_executor.rb', line 32

    def post(*args, &task)
      *args, task = @Wrapper.call(*args, &task)
      @Executor.post(*args, &task)
    end

### #serialized? ⇒ `Boolean`

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.

    43
    44
    45
    # File 'lib/concurrent-ruby-edge/concurrent/executor/wrapping_executor.rb', line 43

    def serialized?
      @Executor.serialized?
    end