Class: Concurrent::SerializedExecution — Concurrent

Class List Method List File List

# Class: Concurrent::SerializedExecution

Inherits:
  Concurrent::Synchronization::LockableObject
  - Object
  - Concurrent::Synchronization::LockableObject
  - Concurrent::SerializedExecution
  show all
Defined in:
  lib/concurrent/executor/serialized_execution.rb

## Overview

Ensures passed jobs in a serialized order never running at the same time.

## Defined Under Namespace

Classes: Job

## Instance Method Summary (collapse)

- - (SerializedExecution) initialize constructor

  A new instance of SerializedExecution.

- - (undocumented) log(level, progname, message = nil, &block) included from Concern::Logging

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

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

  Submit a task to the executor for asynchronous processing.

- - (undocumented) posts(posts)

  As #post but allows to submit multiple tasks at once, it's guaranteed that they will not be interleaved by other tasks.

## Constructor Details

### - (`SerializedExecution`) initialize

Returns a new instance of SerializedExecution

    11
    12
    13
    14
    # File 'lib/concurrent/executor/serialized_execution.rb', line 11

    def initialize()
      super()
      synchronize { ns_initialize }
    end

## Instance Method Details

### - (`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(executor, *args) { ... }

Submit a task to the executor for asynchronous processing.

Parameters:

- executor (`Executor`) —

  to be used for this job

- 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

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

    def post(executor, *args, &task)
      posts [[executor, args, task]]
      true
    end

### - (`undocumented`) posts(posts)

As #post but allows to submit multiple tasks at once, it's guaranteed that they will not be interleaved by other tasks.

Parameters:

- posts (`Array<Array(ExecutorService, Array<Object>, Proc)>`) —

  array of triplets where first is a ExecutorService, second is array of args for task, third is a task (Proc)

    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    # File 'lib/concurrent/executor/serialized_execution.rb', line 44

    def posts(posts)
      # if can_overflow?
      #   raise ArgumentError, 'SerializedExecution does not support thread-pools which can overflow'
      # end

      return nil if posts.empty?

      jobs = posts.map { |executor, args, task| Job.new executor, args, task }

      job_to_post = synchronize do
        if @being_executed
          @stash.push(*jobs)
          nil
        else
          @being_executed = true
          @stash.push(*jobs[1..-1])
          jobs.first
        end
      end

      call_job job_to_post if job_to_post
      true
    end