Module: Concurrent::RubyExecutor

Includes:
Executor, Logging
Included in:
RubySingleThreadExecutor, RubyThreadPoolExecutor, TimerSet, TimerTask
Defined in:
lib/concurrent/executor/executor.rb

Instance Method Summary collapse

Methods included from Logging

#log

Methods included from Executor

#can_overflow?, #serialized?

Instance Method Details

#<<(task) ⇒ self

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



94
95
96
97
# File 'lib/concurrent/executor/executor.rb', line 94

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

#killObject

Begin an immediate shutdown. In-progress tasks will be allowed to complete but enqueued tasks will be dismissed and no new tasks will be accepted. Has no additional effect if the thread pool is not running.



146
147
148
149
150
151
152
153
154
# File 'lib/concurrent/executor/executor.rb', line 146

def kill
  mutex.synchronize do
    break if shutdown?
    stop_event.set
    kill_execution
    stopped_event.set
  end
  true
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



78
79
80
81
82
83
84
85
# File 'lib/concurrent/executor/executor.rb', line 78

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

#running?Boolean

Is the executor running?

Returns:

  • (Boolean)

    ‘true` when running, `false` when shutting down or shutdown



104
105
106
# File 'lib/concurrent/executor/executor.rb', line 104

def running?
  ! stop_event.set?
end

#shutdownObject

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.



131
132
133
134
135
136
137
138
# File 'lib/concurrent/executor/executor.rb', line 131

def shutdown
  mutex.synchronize do
    break unless running?
    stop_event.set
    shutdown_execution
  end
  true
end

#shutdown?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

    ‘true` when shutdown, `false` when shutting down or running



122
123
124
# File 'lib/concurrent/executor/executor.rb', line 122

def shutdown?
  stopped_event.set?
end

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

    ‘true` when not running and not shutdown, else `false`



113
114
115
# File 'lib/concurrent/executor/executor.rb', line 113

def shuttingdown?
  ! (running? || shutdown?)
end

#wait_for_termination(timeout = nil) ⇒ Boolean

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`



167
168
169
# File 'lib/concurrent/executor/executor.rb', line 167

def wait_for_termination(timeout = nil)
  stopped_event.wait(timeout)
end