vineethraj49
09/14/2019, 9:50 AMelizarov
09/14/2019, 9:51 AMjob.compelete()
(or job.cancel()
) and all its launched children complete, too.vineethraj49
09/14/2019, 9:55 AM.isActive
3. I am trying to implement a simple worker pool, post all the `.launch`es complete, the job is still .isActive
, but (see 4)
4. the job is blocking the parent only if the job is spawned with parent=coroutineContext
rather than parent=Dispatchers.Default
5. I am not able to access the .complete()
function on the job interfaceelizarov
09/14/2019, 9:56 AMcomplete
is available on CompletableJob
that the Job(...)
constructor returns.elizarov
09/14/2019, 9:57 AMJob
into a CoroutineScope
(use CoroutineScope(...)
constructor). This way you can do myScope.launch { ... }
which is more idiomatic.vineethraj49
09/14/2019, 10:09 AMkotlinx-coroutines-core:1.1.1
elizarov
09/14/2019, 10:09 AM1.3.0
, then. Do you have any roadblocks?vineethraj49
09/14/2019, 10:10 AMelizarov
09/14/2019, 10:11 AMCompletableDeferred<Unit>()
as your job.vineethraj49
09/14/2019, 10:28 AM: CoroutineScope by CoroutineScope(context)
constructor
2. stored the `.launch`’ed workers in a val workers: List<Job>
3. on my .shutdown()
I do a workers.joinAll()
which waits for completion and
4. .shutdownNow
does workers.map { it.cancelAndJoin() }
vineethraj49
09/14/2019, 10:28 AMvineethraj49
09/14/2019, 10:30 AM.cancelAndJoin()
and just firing .cancel
would suffice for shutdownNow
elizarov
09/14/2019, 10:30 AMcancel
on the scope/job (it cancels all the children) and do job.children.forEach { it.await() }
. No need to keep track of your children manually.vineethraj49
09/14/2019, 10:32 AMcoroutineContext
in the constructor, will that try to cancel children of that context as well?vineethraj49
09/14/2019, 10:34 AMelizarov
09/14/2019, 10:35 AMCoroutineScope(context)
elizarov
09/14/2019, 10:36 AMvineethraj49
09/14/2019, 10:41 AMclass Foo(context) : CoroutineScope by CoroutineScope(context)
will have .[Job]
as whatever is the coroutineContext
passed to the ctor’s .[Job]
is calling Foo().cancel
not going to cancel the other running Job’s in parent?elizarov
09/14/2019, 10:51 AMFoo
, write:
class Foo(context) : CoroutineScope by CoroutineScope(context + Job(context[Job]))
vineethraj49
09/14/2019, 11:16 AMsuspend fun shutdown() {
tasks.close()
coroutineContext[Job]?.children?.forEach { it.join() }
coroutineContext.cancel()
}
fun shutdownNow() {
tasks.close()
coroutineContext.cancel()
}
vineethraj49
09/14/2019, 11:16 AMvineethraj49
09/14/2019, 11:22 AM