I'm a spark Scala Programmer. I have a spark job that has sub-tasks which to complete the whole job. I wanted to use Future
to complete the subtasks in parallel. On completion of the whole job I have to return the whole job response.
What I heard about scala Future
is once the main thread executed and stopped the remaining threads will be killed and also you will get empty response.
I have to use Await.result
to collect the results. But all the blogs are telling that you should avoid Await.result
and it's a bad practice.
Is using Await.result
is correct way of doing or not in my case?
def computeParallel(): Future[String] = {
val f1 = Future { "ss" }
val f2 = Future { "sss" }
val f3 = Future { "ssss" }
for {
r1 <- f1
r2 <- f2
r3 <- f3
} yield (r1 + r2 + r3)
}
computeParallel().map(result => ???)
To my understanding, we have to use Future
in webservice kind of application where it has one process always running that won't be exited. But in my case, once logic execution(scala program) is complete it will exit.
Can I use Future
to my problem or not?