https://kotlinlang.org logo
Title
j

Jgafner

01/28/2021, 7:34 AM
Hi Need some help in understand some basic. I am holding a list of API requests I would like to execute in parallel. I need to wait for all the responses, each response is a list and combine all of them into one list. I still struggle to change the way I think with parallel Vs async - I ll be glad if someone can point me to a similar example in kotlin when async is used to fork and join a collection. thanks
v

vineethraj49

01/28/2021, 7:57 AM
i would assume,
makeManyRequests(list: List<R>) {
  list.map { r -> makeOneRequest(r) }.flatten()
}

// this is unbounded concurrency, do not do this ._.
suspend fun coMakeManyRequests(list: List<R>) = coroutineScope {
  list.map { r -> async { makeOneRequest(r) } }.awaitAll().flatten()
}
?
t

Timo Gruen

01/28/2021, 8:54 AM
why not using the most simple way?
requestList
    .parallelStream()
    .map { request -> client.execute(request) }
    .toList()
l

louiscad

01/28/2021, 2:47 PM
@Timo Gruen Because it's less efficient: blocks threads, meaning less parallelization, so slower execution.
Also streams are JVM only
t

Timo Gruen

01/28/2021, 2:53 PM
True, havn’t considered the blocking of the threads. Sure coroutines would be better I guess. Could you paste the corresponding example, how it would look like with Kotlin coroutines @louiscad?
l

louiscad

01/28/2021, 2:56 PM
The snippet in this thread under "this is unbounded concurrency, do not do this ._." is actually correct so long as the list of request to do is not higher than the system can handle
🙏 1
j

Jgafner

01/28/2021, 3:49 PM
thank you all