Reputation: 272
This is probably a simple problem to the scala educated mind but I'm still a beginner ;)
I have a base actor who dispatches a task to multiple worker actors and replies it's result to a blocking external call via !?
a = new a
a.start
println(a !? "12345")
class a extends Actor {
def act = {
loop {
react {
case msg =>
val result = worker_actor_1 !? msg
result += worker_actor_2 !? msg
result += worker_actor_3 !? msg
// So I just have multiple workers who should do stuff in parallel and the aggregated result should be returned to the calling function
reply(result)
}
Now I don't know how to truly parallelize the worker actors in the blocking call because in the end I have to reply(). The calling entitiy is no actor, just a regular class.
Upvotes: 4
Views: 253
Reputation: 13221
You can create several futures and then spawn a separate actor to wait for their results. Thus your dispatch will be ready for new requests. The snippet of code follows:
case msg =>
val invoker = sender
val flist =
worker_actor_1 !! task1 ::
worker_actor_2 !! task2 ::
worker_actor_3 !! task3 :: Nil
Scheduler.execute { invoker ! Futures.awaitAll(100, flist).map{ ..sum the results.. } }
Please note awaitAll
returns List[Option[Any]]
, so you can learn if something went wrong and your worker actors did not accomplish the task in time
Upvotes: 2