bbaldino
11/30/2020, 5:41 PMCoroutingScope#onFinish
helper which delayed wrapped an indefinite delay in a try/finally and called a cleanup method in the finally
block, but it feels wrong).
Of these, I think #2 is the biggest sticking point: I find myself wishing I could create a child coroutine scope but wait for any child to finish, instead of all children to finish (like coroutineScope
and runBlocking
do). Does that exist?louiscad
11/30/2020, 6:16 PMraceOf
from Splitties coroutines might be what you're looking for.
For the part 4, it depends on where you put the cleanup code (likely in a finally block).marstran
11/30/2020, 6:16 PMlaunch
inside channelFlow
right now, and it seems to act the way you want. I used first()
on the resulting flow to get the first element to finish.
This is what I did:
val tasks = // List of tasks
val parent = launch {
channelFlow {
tasks.forEach { task ->
launch {
send(task.run())
}
}
}.first()
}
parent.invokeOnCompletion {
println("Cleanup")
}
bbaldino
11/30/2020, 6:17 PMraceOf
. I did do work in a finally block, but since it blocks the other coroutines from starting cleanup it wasn't a great fit. Not sure the best way to get a different behavior there.bbaldino
11/30/2020, 6:18 PMlouiscad
11/30/2020, 6:19 PMGlobalScope
) if the temporary leak is not an issue. @bbaldinomarstran
11/30/2020, 6:19 PMchannelFlow
will automatically cancel all child-jobs when first()
returns. If something fails first, then that exception will bubble up and cause all the others to be cancelled as well.bbaldino
11/30/2020, 6:20 PMmarstran
11/30/2020, 6:20 PMsupervisorScope
inside your parent if you want more control of the error-handling.bbaldino
11/30/2020, 6:21 PMlouiscad
11/30/2020, 6:23 PMraceOf
function you can use via the depends or copy pasting: https://github.com/LouisCAD/Splitties/tree/main/modules/coroutines#racing-coroutinesbbaldino
11/30/2020, 6:44 PMmarstran
11/30/2020, 6:47 PMlaunch
to the task, so it can create more child-tasks on it.marstran
11/30/2020, 6:48 PMlaunch { task.run(this) }
bbaldino
11/30/2020, 6:48 PMlouiscad
11/30/2020, 6:50 PMCoroutineScope
as it's the receiverbbaldino
11/30/2020, 6:52 PMlouiscad
11/30/2020, 6:54 PMcoroutineScope { }
if they have child coroutines to launch)bbaldino
11/30/2020, 6:54 PMcoroutineScope
bbaldino
11/30/2020, 6:55 PM