manually-with-scope with child parallelization launched co-routines
Here we manually setup scope and wait on it for launched actions to finish - a bit cumbersome but works.
Code
package com.glassthought.sandbox
import gt.sandbox.util.output.Out
import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.seconds
private val out = Out.standard()
fun main(): Unit = runBlocking {
out.info("START - ON MAIN")
try {
mainImpl(out)
} catch (e: Exception) {
out.error("back at MAIN got an exception! of type=[${e::class.simpleName}] with msg=[${e.message}] cause=[${e.cause}]. Exiting with error code 1")
exitProcess(1)
}
out.info("DONE - WITHOUT errors on MAIN")
}
private suspend fun mainImpl(out: Out) {
coroutineScope {
foo("msg-1")
}
}
private suspend fun foo(msg: String) {
out.actionWithMsg("fooImpl", { fooImpl(msg) })
}
private suspend fun fooImpl(msg: String) {
val job = Job()
val scope = CoroutineScope(coroutineContext + job)
scope.launch {
out.delayNamed(3.seconds, "delayed([${msg}])")
}
scope.launch {
out.delayNamed(2.seconds, "delayed([${msg}])")
}
scope.launch {
out.delayNamed(1.seconds, "delayed([${msg}])")
}
out.actionWithMsg("job.complete()", { job.complete() })
out.actionWithMsg("job.join()", { job.join() })
}
Command to reproduce:
gt.sandbox.checkout.commit 800315e25e0cc39077c2 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run --quiet"
Recorded output of command:
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
[INFO][elapsed: 37ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] START - ON MAIN
[INFO][elapsed: 52ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [>] Starting action=[fooImpl]
[INFO][elapsed: 54ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [>] Starting action=[job.complete()]
[INFO][elapsed: 55ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [<] Finished action=[job.complete()].
[INFO][elapsed: 55ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [>] Starting action=[job.join()]
[INFO][elapsed: 59ms][🥇][⓶][coroutname:@coroutine#2][tname:main/tid:1] Delaying for 3s what_for=[delayed([msg-1])]
[INFO][elapsed: 60ms][🥇][⓷][coroutname:@coroutine#3][tname:main/tid:1] Delaying for 2s what_for=[delayed([msg-1])]
[INFO][elapsed: 61ms][🥇][⓸][coroutname:@coroutine#4][tname:main/tid:1] Delaying for 1s what_for=[delayed([msg-1])]
[INFO][elapsed: 1062ms][🥇][⓸][coroutname:@coroutine#4][tname:main/tid:1] Done delaying for 1s what_for=[delayed([msg-1])]
[INFO][elapsed: 2061ms][🥇][⓷][coroutname:@coroutine#3][tname:main/tid:1] Done delaying for 2s what_for=[delayed([msg-1])]
[INFO][elapsed: 3060ms][🥇][⓶][coroutname:@coroutine#2][tname:main/tid:1] Done delaying for 3s what_for=[delayed([msg-1])]
[INFO][elapsed: 3061ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [<] Finished action=[job.join()].
[INFO][elapsed: 3061ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] [<] Finished action=[fooImpl].
[INFO][elapsed: 3061ms][🥇][①][coroutname:@coroutine#1][tname:main/tid:1] DONE - WITHOUT errors on MAIN
Backlinks