https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Rob Elliot

05/19/2020, 9:36 PM
Hi all, I found myself writing this, then was confused that it wasn’t in the coroutines library already - is it a bad idea / implementation?
Copy code
suspend fun <T> Iterable<T>.forEachParallel(
  f: suspend (T) -> Unit
) {
  coroutineScope {
    forEach { t ->
      launch {
        f(t)
      }
    }
  }
}
o

octylFractal

05/19/2020, 9:40 PM
I think part of it is that it's a quick fix, but in the big picture, not a good fix -- it has no support for limiting how many things are launched, so it will just fill the dispatcher queue if you're processing too many things. sure, something good could be designed, but the coroutines team just hasn't gotten a "parallel processing" design story done yet I think. See https://github.com/Kotlin/kotlinx.coroutines/issues/1147, which would also be somewhat applicable here, specifically https://github.com/Kotlin/kotlinx.coroutines/issues/1147#issuecomment-517829768
r

Rob Elliot

05/19/2020, 9:42 PM
OK, thanks for the heads up
4 Views