4

I want to run to suspend functions in parallel and return result once the faster one of them returns result. I tried doing the following, but I think it only returns when both of them have finished and it takes too much time, since I'm waiting for both.

val coroutineScope = CoroutineScope(Dispatchers.Main)
val a = coroutineScope.async {
    a(context)
}
val b = coroutineScope.async {
    b(context)
}

val results = listOf(a, b).awaitAll()

return if (results.any { it is RunSuccess }) {
    ...
} else {
    ...
}

Any ideas?

ZookKep
  • 481
  • 5
  • 13

2 Answers2

3

You can use select as follows :

suspend fun doWork(): String = coroutineScope {
   select<String> {
      async { work1() }.onAwait { it }
      async { work2() }.onAwait { it }
   }.also {
      coroutineContext.cancelChildren()
   }
}

On this example is returning a String but you can change it with whatever you want, it depends on what your work is returning.

In case you are looking for more functional programming version you can use raceN from Arrow

Where you have this method

public suspend inline fun <A, B> raceN(crossinline fa: suspend CoroutineScope.() -> A, crossinline fb: suspend CoroutineScope.() -> B): Either<A, B> =
  raceN(Dispatchers.Default, fa, fb)

And then you call this raceN method

public suspend inline fun <A, B> raceN(
  ctx: CoroutineContext = EmptyCoroutineContext,
  crossinline fa: suspend CoroutineScope.() -> A,
  crossinline fb: suspend CoroutineScope.() -> B
): Either<A, B> =
  coroutineScope {
    val a = async(ctx) { fa() }
    val b = async(ctx) { fb() }
    select<Either<A, B>> {
      a.onAwait.invoke { Either.Left(it) }
      b.onAwait.invoke { Either.Right(it) }
    }.also {
      when (it) {
        is Either.Left -> b.cancelAndJoin()
        is Either.Right -> a.cancelAndJoin()
      }
    }
  }
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 1
    Thank you. Does the `select` answer mean that both the processes will run until the end though? I mean, I should just use the first one that finishes as soon as it finishes and the second one should continue running in the background - not stop the running of the second one, right? – ZookKep Aug 26 '22 at 09:07
  • 1
    No this first solution with `select` stops the running of the second one – Littlish Aug 26 '22 at 12:34
1

Arrow fx has a nice method called raceN.

https://arrow-kt.io/docs/apidocs/arrow-fx-coroutines/arrow.fx.coroutines/race-n.html

Daniel Jacob
  • 1,455
  • 9
  • 17