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/behaviour/supervised.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/terminates_children.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



38
39
40
# File 'lib/concurrent/actor.rb', line 38

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

.i_know_it_is_experimental!Object

call this to disable experimental warning



99
100
101
# File 'lib/concurrent/actor.rb', line 99

def self.i_know_it_is_experimental!
  warn 'Method Actor.i_know_it_is_experimental! is deprecated. The Actors are no longer experimental.'
end

.rootObject

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



49
50
51
# File 'lib/concurrent/actor.rb', line 49

def self.root
  @root.value!
end

.spawn(*args, &block) ⇒ Reference

Spawns a new actor.

Examples:

simple

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

complex

Actor.spawn name:     :ping3,
              class:    AdHoc,
              args:     [1]
              executor: Concurrent.configuration.global_task_pool do |add|
  lambda { |number| number + add }
end

Parameters:

Returns:



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

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

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

as spawn but it’ll raise when Actor not initialized properly



78
79
80
# File 'lib/concurrent/actor.rb', line 78

def self.spawn!(*args, &block)
  spawn(spawn_optionify(*args).merge(initialized: ivar = IVar.new), &block).tap { ivar.no_error! }
end

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

Overloads:



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

def self.spawn_optionify(*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