Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waiting for Actor termination? #825

Open
mvgijssel opened this issue Sep 18, 2019 · 1 comment
Open

Waiting for Actor termination? #825

mvgijssel opened this issue Sep 18, 2019 · 1 comment
Labels
question An user question, does not change the library.

Comments

@mvgijssel
Copy link

Is there an easy way to wait for an Actor to terminate? I'm aware that I can wait till all messages are processed:

actor = MyActor.spawn(name: 'actor')
actor << :dostuff
actor.ask(:await).wait

or wait for the actor to respond (which AFAIK is the same)

actor = MyActor.spawn(name: 'actor')
actor << :dostuff
actor.ask(:some_result).value # or actor.ask!(:some_result)

but both are not what I'm looking for. I want to know how to block until an actor is terminated when the actor has asynchronous execution itself:

class MyActor < Concurrent::Actor::Context
  def on_message(message)
    case message
    when :start
      promises = 10.times.map do |n|
        Concurrent::Promises.future do
          sleep 1
          n
        end
      end

      Concurrent::Promises.zip(*promises).then do
        self.tell :terminate!
      end

      Concurrent::Actor::Behaviour::MESSAGE_PROCESSED
    else
      pass
    end
  end
end

my_actor = MyActor.spawn(name: 'actor')
my_actor.tell(:start)

# How to do this, as this is obviously not great:
loop do
  break if my_actor.ask!(:terminated?)
end
@pitr-ch pitr-ch added the question An user question, does not change the library. label Feb 10, 2020
@pitr-ch
Copy link
Member

pitr-ch commented Feb 10, 2020

The newer Actor implementation which has exact same behaviour as Erlang is more thought out in that respect and has http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ErlangActor/Pid.html#terminated-instance_method
In the old Actors you can either create a different actor and link it or ask for a termination event with :termination_event message. Then you can block with wait on the event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question An user question, does not change the library.
Projects
None yet
Development

No branches or pull requests

2 participants