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,
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()
}
@Timo Gruen Because it's less efficient: blocks threads, meaning less parallelization, so slower execution.
louiscad
01/28/2021, 2:47 PM
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