Class: Concurrent::Actor Deprecated
- Inherits:
-
Object
- Object
- Concurrent::Actor
- Defined in:
- lib/concurrent/actor/actor.rb
Overview
Actor is being replaced with a completely new framework prior to v1.0.0
Actor-based concurrency is all the rage in some circles. Originally described in 1973, the actor model is a paradigm for creating asynchronous, concurrent objects that is becoming increasingly popular. Much has changed since actors were first written about four decades ago, which has led to a serious fragmentation within the actor community. There is no universally accepted, strict definition of “actor” and actor implementations differ widely between languages and libraries.
A good definition of “actor” is:
An independent, concurrent, single-purpose, computational entity that communicates exclusively via passing.
The Concurrent::Actor class in this library is based solely on the Actor trait defined in the Scala standard library. It does not implement all the features of Scala’s Actor but its behavior for what has been implemented is nearly identical. The excluded features mostly deal with Scala’s message semantics, strong typing, and other characteristics of Scala that don’t really apply to Ruby.
Unlike many of the abstractions in this library, Actor takes an object-oriented approach to asynchronous concurrency, rather than a *functional programming* approach.
Because Actor mixes in the Concurrent::Runnable module subclasses have access to the #on_error method and can override it to implement custom error handling. The Actor base class does not use #on_error so as to avoid conflit with subclasses which override it. Generally speaking, #on_error should not be used. The Actor base class provides concictent, reliable, and robust error handling already, and error handling specifics are tied to the message posting method. Incorrect behavior in an #on_error override can lead to inconsistent Actor behavior that may lead to confusion and difficult debugging.
The Actor superclass mixes in the Ruby standard library Observable module to provide consistent callbacks upon message processing completion. The normal Observable methods, including #add_observer behave normally. Once an observer is added to an Actor it will be notified of all messages processed after addition. Notification will not occur for any messages that have already been processed.
Observers will be notified regardless of whether the message processing is successful or not. The #update method of the observer will receive four arguments. The appropriate method signature is:
def update(time, , result, reason)
These four arguments represent:
-
The time that message processing was completed
-
An array containing all elements of the original message, in order
-
The result of the call to
#act(will benilif an exception was raised) -
Any exception raised by
#act(ornilif message processing was successful)
Class Method Summary collapse
-
.pool(count, *args, &block) ⇒ Array
deprecated
Deprecated.
Actoris being replaced with a completely new framework prior to v1.0.0
Instance Method Summary collapse
-
#act(*message) ⇒ Object
deprecated
Deprecated.
Actoris being replaced with a completely new framework prior to v1.0.0
Methods included from Runnable
included, #run, #run!, #running?, #stop
Methods included from Postable
#<<, #forward, #post, #post!, #post?, #ready?
Class Method Details
.pool(count, *args, &block) ⇒ Array
Actor is being replaced with a completely new framework prior to v1.0.0
Create a pool of actors that share a common mailbox.
Every Actor instance operates on its own thread. When one thread isn’t enough capacity to manage all the messages being sent to an Actor a pool can be used instead. A pool is a collection of Actor instances, all of the same type, that shate a message queue. Messages from other threads are all sent to a single queue against which all Actors load balance.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/concurrent/actor/actor.rb', line 181 def self.pool(count, *args, &block) warn '[DEPRECATED] `Actor` is deprecated and will be replaced with `Actress`.' raise ArgumentError.new('count must be greater than zero') unless count > 0 mailbox = Queue.new actors = count.times.collect do if block_given? actor = self.new(*args, &block.dup) else actor = self.new(*args) end actor.instance_variable_set(:@queue, mailbox) actor end return Poolbox.new(mailbox), actors end |
Instance Method Details
#act(*message) ⇒ Object
Actor is being replaced with a completely new framework prior to v1.0.0
Actors are defined by subclassing the Concurrent::Actor class and overriding the #act method. The #act method can have any signature/arity but def act(*args) is the most flexible and least error-prone signature. The #act method is called in response to a message being post to the Actor instance (see Behavior below).
214 215 216 217 |
# File 'lib/concurrent/actor/actor.rb', line 214 def act(*) warn '[DEPRECATED] `Actor` is deprecated and will be replaced with `Actress`.' raise NotImplementedError.new("#{self.class} does not implement #act") end |