Home > Software design >  Sending multiple requests simultaneously and waiting to collect results
Sending multiple requests simultaneously and waiting to collect results

Time:01-08

I am working on a project which involves a lot of tasks of sending multiple requests at the same time and waiting to collect results. A general scheme can be described in the following code:

for(i=0; i<job_number; i  )
    //send job i to remote machine to do long computation(an rpc call)

//wait for all remote job to finish, collect all results from rpc calls
//do aggregation based on the collected results

Based on my research, there are several possible solutions:

  1. Making the rpc calls asynchronous, when a call is done it will return a future object and the following computation will wait until the content of the future object is obtained.
  2. Instead of loop, do a map operation to "broadcast" the rpc call to all the targets at the same time, and wait for all the responses(but I am not sure how to do this, all the requests have to go through the same socket connection, is it possible to have multiple threads sharing the same socket connection sending out requests and receiving the responses at the same time?)

How is such problem being handled in practice? Which solution is better or more commonly used. Are there any other possible solutions? Thanks.

CodePudding user response:

I would create a CompletableFuture for each RPC and collect all of them in a list futures:

for(i=0; i<job_number; i  )
    futures.add(CompletableFuture.runAsync(/* Runnable for job i*/));

You can then reduce the Stream<CompletableFuture<T>> futures.stream to a CompletableFuture<Stream<T>> as shown in this answer.

  • Related