Module: Concurrent::Actor

Defined in:
lib/concurrent-ruby-edge/concurrent/actor.rb,
lib/concurrent-ruby-edge/concurrent/actor/core.rb,
lib/concurrent-ruby-edge/concurrent/actor/root.rb,
lib/concurrent-ruby-edge/concurrent/actor/utils.rb,
lib/concurrent-ruby-edge/concurrent/actor/errors.rb,
lib/concurrent-ruby-edge/concurrent/actor/context.rb,
lib/concurrent-ruby-edge/concurrent/actor/envelope.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour.rb,
lib/concurrent-ruby-edge/concurrent/actor/reference.rb,
lib/concurrent-ruby-edge/concurrent/actor/type_check.rb,
lib/concurrent-ruby-edge/concurrent/actor/utils/pool.rb,
lib/concurrent-ruby-edge/concurrent/actor/utils/ad_hoc.rb,
lib/concurrent-ruby-edge/concurrent/actor/utils/balancer.rb,
lib/concurrent-ruby-edge/concurrent/actor/utils/broadcast.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/awaits.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/buffer.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/pausing.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/abstract.rb,
lib/concurrent-ruby-edge/concurrent/actor/public_delegations.rb,
lib/concurrent-ruby-edge/concurrent/actor/internal_delegations.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/supervising.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/termination.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/sets_results.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/removes_child.rb,
lib/concurrent-ruby-edge/concurrent/actor/behaviour/executes_context.rb,
lib/concurrent-ruby-edge/concurrent/actor/default_dead_letter_handler.rb,
lib/concurrent-ruby-edge/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



34
35
36
# File 'lib/concurrent-ruby-edge/concurrent/actor.rb', line 34

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

.rootObject

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



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

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 available 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:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/concurrent-ruby-edge/concurrent/actor.rb', line 72

def self.spawn(*args, &block)
  options = to_spawn_options(*args)
  if options[:executor] && options[:executor].is_a?(ImmediateExecutor)
    raise ArgumentError, 'ImmediateExecutor is not supported'
  end
  if Actor.current
    Core.new(options.merge(parent: Actor.current), &block).reference
  else
    root.ask([:spawn, options, 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



85
86
87
# File 'lib/concurrent-ruby-edge/concurrent/actor.rb', line 85

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

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

Overloads:



96
97
98
99
100
101
102
103
104
# File 'lib/concurrent-ruby-edge/concurrent/actor.rb', line 96

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