Multiple actor call from blocking call

This is probably a simple problem for the educated mind of scala, but I'm still a beginner;)

I have a basic actor who submits a task to several acting actors and answers that this leads to a blocking external call through !?

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 do not know how to really parallelize the existing participants in a blocking call, because in the end I have to answer (). The caller is not an actor, just an ordinary class.

+4
source share
1 answer

You can create several futures and then create a separate actor to wait for the results. Thus, your dispatch will be ready for new requests. The following is a snippet of code:

 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.. } } 

Note that awaitAll returns a List[Option[Any]] , so you can find out that something went wrong and your actor workers did not complete the task on time

+2
source

All Articles