How do you know when the chord actors have finished?

I am trying to implement a simple application where I assign different tasks to Akka Akkas and let them compute the results themselves. The problem is how to determine when they all finished their assigned task (successes or failures), since I need to get the computation results from them. What's the typical approach?

I tried using system.shutdown () to wait for all the participants to be done, but past this command, the participants are already complete and are not responding to any messages needed to get the computation results.

Another thing I thought of was sending something like a JobCompleted message from the current members to their parents. Then I could count how many messages of this kind I get back, and if the score is equal to the actors' players, we know they are all finished. Although I do not know what happens when crashes occur inside the actors. Also this approach seems too clumsy.

+3


source to share


3 answers


Possible duplicates:

Know when the actors are finished

Tracking Akka when actors have finished




The easiest option is to use a request template and wait for all futures returned. Either in a block or via Future.sequence (See the official docs for more info)

Also, if you need to terminate immediately after completing your task, there are many different approaches. For example see http://letitcrash.com/post/30165507578/shutdown-patterns-in-akka-2

+2


source


When you create "assignments" and submit them to acting actors, you can create future results using a query template.

It looks something like this:

val myActorRef = system.actorOf(Props(classOf[WorkerActor]), "worker")

val result = (myActorRef ? SomeComputationMessage()).mapTo[Result]

      



if the actor sends its result back to the caller with sender ! Result()

then this query pattern will return the future with the result

Since these are results futures, you can match them and work with them accordingly based on success / failure

http://doc.akka.io/api/akka/2.0/akka/pattern/package.html

0


source


I would have a coordinating actor that spawns workers and then sends each message indicating the work it should do, creating a set containing ActorRefs for each worker. When the coordinator receives a JobCompleted message (or JobFailed if necessary), copy the results and remove that message sender (for example, ActorRef WorkerRef) from the set. When the set is empty, all workers are complete. The workers themselves can simply call context.stop(self)

to finish themselves as soon as they have sent their message.

Further semantics will depend on the specifics of the work. For example, the coordinator can set up a scheduled call by itself (for example, the call context.system.scheduler.schedule(someDelay, someDelay, self, ResendWorkOrders)

will send a ResendWorkOrders message to itself after each interval someDelay

). When he receives this call, he can re-send work requests to every Worker remaining in the set (or even respawn them). When the set is empty (all workers are complete), scheduling can be canceled (the scheduler call returns a Cancellable

). This can handle, for example, cases where the delivery of a message to a worker (for example, in a distributed system) might fail, or when a worker might crash or log off without responding to the coordinator.

0


source







All Articles