Hi Need some help in understand some basic. I am h...
# coroutines
j
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
i would assume,
Copy code
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
why not using the most simple way?
Copy code
requestList
    .parallelStream()
    .map { request -> client.execute(request) }
    .toList()
l
@Timo Gruen Because it's less efficient: blocks threads, meaning less parallelization, so slower execution.
Also streams are JVM only
t
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
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
thank you all