What’s the best way of spawning children coroutine...
# coroutines
j
What’s the best way of spawning children coroutines that do not propagate cancellation upwards. I don’t want to wait for completion and don’t care about canceling either. a) use a dummy Supervisor b) use GlobalScope.launch c) register a CoroutineExceptionHandler on the launch builder
a
Supervisor is supposed to work and there’s also
supervisorScope
. However, I find that in many cases it doesn’t catch everything, so
CoroutineExceptionHandler
will work
Global scope changes your lifecycle which you’d probably want to avoid, and even then an exception could end up propagating upwards (from my experience)
e
I would first enquire what is your use-cases. What are you trying to achieve? (the answer will depend on that)
1
j
It’s for writing an Amazon SQS consumer. This is what I’ve got so far.
The idea being that the coroutine that retrieves the messages spawns children coroutines for processing, but does not wait for the result.
@elizarov I’d love to know your thoughts on this one
e
When you say “don’t propagete upwards”, you mean “don’t cancel their peers on failure?“. Use
SupervisorJob()
for that!
👍 1
j
Thanks!