Class: Concurrent::Async::AsyncDelegator
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Concurrent::Synchronization::LockableObject
|
Defined in: | lib/concurrent-ruby/concurrent/async.rb |
Overview
Delegates asynchronous, thread-safe method calls to the wrapped object.
Class Method Summary
-
.new(delegate) ⇒ AsyncDelegator
constructor
Create a new delegator object wrapping the given delegate.
Instance Method Summary
-
#method_missing(method, *args, &block) ⇒ IVar
Delegates method calls to the wrapped object.
-
#perform
Perform all enqueued tasks.
- #reset_if_forked
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Check whether the method is responsive.
Synchronization::LockableObject
- Inherited
Constructor Details
.new(delegate) ⇒ AsyncDelegator
Create a new delegator object wrapping the given delegate.
# File 'lib/concurrent-ruby/concurrent/async.rb', line 288
def initialize(delegate) super() @delegate = delegate @queue = [] @executor = Concurrent.global_io_executor @ruby_pid = $$ end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ IVar
Delegates method calls to the wrapped object.
# File 'lib/concurrent-ruby/concurrent/async.rb', line 305
def method_missing(method, *args, &block) super unless @delegate.respond_to?(method) Async::validate_argc(@delegate, method, *args) ivar = Concurrent::IVar.new synchronize do reset_if_forked @queue.push [ivar, method, args, block] @executor.post { perform } if @queue.length == 1 end ivar end
Instance Method Details
#perform
Perform all enqueued tasks.
This method must be called from within the executor. It must not be called while already running. It will loop until the queue is empty.
# File 'lib/concurrent-ruby/concurrent/async.rb', line 330
def perform loop do ivar, method, args, block = synchronize { @queue.first } break unless ivar # queue is empty begin ivar.set(@delegate.send(method, *args, &block)) rescue => error ivar.fail(error) end synchronize do @queue.shift return if @queue.empty? end end end
#reset_if_forked
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/async.rb', line 348
def reset_if_forked if $$ != @ruby_pid @queue.clear @ruby_pid = $$ end end
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Check whether the method is responsive
# File 'lib/concurrent-ruby/concurrent/async.rb', line 322
def respond_to_missing?(method, include_private = false) @delegate.respond_to?(method) || super end