ansman
03/01/2019, 10:14 PMcoroutineScope {
launch(start = LAZY) {}
}
bdawg.io
03/02/2019, 12:33 AMwon't return until all children jobs complete
but there’s additional caveats that simple definition doesn’t spell out. You would need to do something with that lazy job so it knows what to do about being no longer lazyansman
03/02/2019, 12:35 AMbdawg.io
03/02/2019, 12:35 AMansman
03/02/2019, 12:36 AMansman
03/02/2019, 12:37 AMbdawg.io
03/02/2019, 12:41 AMwon't return until all activated jobs have completed
. I think it’s a bug to create a job and never do anything with it. Structured concurrency IMO means that because it’s structured, you have expected behavior without assumptions (including to cancel jobs that have not yet been started, because the job was created for some reason).bdawg.io
03/02/2019, 12:42 AMI’m not saying it’s not doing a logical thingYeah, I definitely agree
There’s additional caveats that simple definition doesn’t spell outBut I also have never had an instance where I tried to create a lazy job within such a confined scope via
coroutineScope { ... }
bdawg.io
03/02/2019, 12:45 AMto cancel jobs that have not yet been started
suspend fun CoroutineScope.cancelUnstartedJobs() {
children.filter {
!it.isCancelled && !it.isActive && !it.isCompleted
}.forEach {
it.cancelAndJoin()
}
}
ansman
03/02/2019, 12:51 AMlouiscad
03/02/2019, 8:23 AMlouiscad
03/02/2019, 8:23 AMansman
03/02/2019, 4:35 PMval progressShown = launch(start = CoroutineStart.LAZY) { delay(1000) }
val showProgress = launch {
delay(1000)
view.showImportProgress()
progressShown.start()
}
val hideProgress = launch(start = CoroutineStart.LAZY) {
if (showProgress.isCompleted) {
try {
progressShown.join()
} finally {
view.hideImportProgress()
}
} else {
showProgress.cancel()
// I had forgotten this so the coroutine never completed
progressShown.cancel()
}
}
// Do long running task
hideProgress.start()
hideProgress.join()
ansman
03/02/2019, 4:36 PMgroostav
03/03/2019, 8:18 PMunder cercomstances XYZ my library should hold parent scope openUltimately I was thinking about trying to attach some kind of listener to the completion of the parent to cancel my unstarted/long-running jobs when the parent finishes its scope block, but decided that behaviour would probably be even more confusing.
groostav
03/03/2019, 8:20 PMjstack
and/or intelliJ's own "pause" on a thread pointing at a runBlocking
event-loop have the resulting exception contain all the jobs the runBlocking
loop is waiting on.ansman
03/03/2019, 9:22 PM