-
Notifications
You must be signed in to change notification settings - Fork 419
Open
Labels
questionAn user question, does not change the library.An user question, does not change the library.
Description
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
Metadata
Metadata
Assignees
Labels
questionAn user question, does not change the library.An user question, does not change the library.