Module: Concurrent::Actor

Defined in:
lib/concurrent/actor.rb,
lib/concurrent/actor/core.rb,
lib/concurrent/actor/root.rb,
lib/concurrent/actor/utils.rb,
lib/concurrent/actor/errors.rb,
lib/concurrent/actor/context.rb,
lib/concurrent/actor/envelope.rb,
lib/concurrent/actor/behaviour.rb,
lib/concurrent/actor/reference.rb,
lib/concurrent/actor/type_check.rb,
lib/concurrent/actor/utils/pool.rb,
lib/concurrent/actor/utils/ad_hoc.rb,
lib/concurrent/actor/utils/balancer.rb,
lib/concurrent/actor/utils/broadcast.rb,
lib/concurrent/actor/behaviour/awaits.rb,
lib/concurrent/actor/behaviour/buffer.rb,
lib/concurrent/actor/behaviour/linking.rb,
lib/concurrent/actor/behaviour/pausing.rb,
lib/concurrent/actor/behaviour/abstract.rb,
lib/concurrent/actor/public_delegations.rb,
lib/concurrent/actor/internal_delegations.rb,
lib/concurrent/actor/behaviour/supervising.rb,
lib/concurrent/actor/behaviour/termination.rb,
lib/concurrent/actor/behaviour/sets_results.rb,
lib/concurrent/actor/behaviour/removes_child.rb,
lib/concurrent/actor/behaviour/executes_context.rb,
lib/concurrent/actor/default_dead_letter_handler.rb,
lib/concurrent/actor/behaviour/errors_on_unknown_message.rb

Overview

Defined Under Namespace

Modules: Behaviour, InternalDelegations, PublicDelegations, TypeCheck, Utils Classes: AbstractContext, ActorTerminated, Context, Core, DefaultDeadLetterHandler, Envelope, Reference, RestartingContext, Root, UnknownMessage

Constant Summary collapse

Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.currentReference?

Returns current executing actor if any.

Returns:

  • (Reference, nil)

    current executing actor if any



33
34
35
# File 'lib/concurrent/actor.rb', line 33

def self.current
  Thread.current[:__current_actor__]
end

.rootObject

A root actor, a default parent of all actors spawned outside an actor



44
45
46
# File 'lib/concurrent/actor.rb', line 44

def self.root
  @root.value!
end

.spawn(*args, &block) ⇒ Reference

Spawns a new actor. Concurrent::Actor::AbstractContext.spawn allows to omit class parameter. To see the list of avaliable options see Concurrent::Actor::Core#initialize

Examples:

by class and name

Actor.spawn(AdHoc, :ping1) { -> message { message } }

by option hash

inc2 = Actor.spawn(class:    AdHoc,
                   name:     'increment by 2',
                   args:     [2],
                   executor: Concurrent.global_io_executor) do |increment_by|
  lambda { |number| number + increment_by }
end
inc2.ask!(2) # => 4

Parameters:

Returns:

See Also:



67
68
69
70
71
72
73
# File 'lib/concurrent/actor.rb', line 67

def self.spawn(*args, &block)
  if Actor.current
    Core.new(to_spawn_options(*args).merge(parent: Actor.current), &block).reference
  else
    root.ask([:spawn, to_spawn_options(*args), block]).value!
  end
end

.spawn!(*args, &block) ⇒ Object

as spawn but it’ll block until actor is initialized or it’ll raise exception on error



76
77
78
# File 'lib/concurrent/actor.rb', line 76

def self.spawn!(*args, &block)
  spawn(to_spawn_options(*args).merge(initialized: future = Concurrent.future), &block).tap { future.wait! }
end

.to_spawn_options(context_class, name, *args) ⇒ Object .to_spawn_options(opts) ⇒ Object

Overloads:



87
88
89
90
91
92
93
94
95
# File 'lib/concurrent/actor.rb', line 87

def self.to_spawn_options(*args)
  if args.size == 1 && args.first.is_a?(::Hash)
    args.first
  else
    { class: args[0],
      name:  args[1],
      args:  args[2..-1] }
  end
end