Class: Concurrent::OneByOne

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

Overview

Ensures that jobs are passed to the given executors one by one, never running at the same time.

Defined Under Namespace

Classes: Job

Instance Method Summary collapse

Constructor Details

#initializeOneByOne



13
14
15
16
17
# File 'lib/concurrent/executor/one_by_one.rb', line 13

def initialize
  @being_executed = false
  @stash          = []
  @mutex          = Mutex.new
end

Instance Method Details

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

Submit a task to the executor for asynchronous processing.

Yields:

  • the asynchronous task to perform

Raises:

  • (ArgumentError)

    if no task is given



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/concurrent/executor/one_by_one.rb', line 31

def post(executor, *args, &task)
  return nil if task.nil?
  # FIXME Agent#send-off will blow up here
  # if executor.can_overflow?
  #   raise ArgumentError, 'OneByOne cannot be used in conjunction with executor which may overflow'
  # end

  job = Job.new executor, args, task

  begin
    @mutex.lock
    post = if @being_executed
             @stash << job
             false
           else
             @being_executed = true
           end
  ensure
    @mutex.unlock
  end

  call_job job if post
  true
end