module Concurrent::Actor
{include:file:docs-source/actor/main.md} @api Actor
@!macro warn.edge
Constants
- Error
Public Class Methods
Source
# File lib/concurrent-ruby-edge/concurrent/actor.rb, line 34 def self.current Thread.current[:__current_actor__] end
@return [Reference, nil] current executing actor if any
Source
# File lib/concurrent-ruby-edge/concurrent/actor.rb, line 49 def self.root @root.value! end
A root actor, a default parent of all actors spawned outside an actor
Source
# 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
Spawns a new actor. {Concurrent::Actor::AbstractContext.spawn} allows to omit class parameter. To see the list of available options see {Core#initialize} @see Concurrent::Actor::AbstractContext.spawn
@see Core#initialize @example by class and name
Actor.spawn(AdHoc, :ping1) { -> message { message } }
@example 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
@param block for context_class instantiation @param args see {.to_spawn_options} @return [Reference] never the actual actor
Source
# 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
as {.spawn} but it’ll block until actor is initialized or it’ll raise exception on error
Source
# 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
@overload to_spawn_options
(context_class, name, *args)
@param [AbstractContext] context_class to be spawned @param [String, Symbol] name of the instance, it's used to generate the {Core#path} of the actor @param args for context_class instantiation
@overload to_spawn_options
(opts)
see {Core#initialize} opts