<@U2E974ELT> if I have to pick between doing a glo...
# coroutines
a
@elizarov if I have to pick between doing a globalscope launch vs launching a thread and inside that doing a withContext Dispatchers io launch - which one would be preferred? Context: cannot use run blocking on the calling thread and it is not a suspending function but want to launch multiple coroutines to do some stuff in parallel
z
You can just launch directly on the IO dispatcher from whatever thread.
Copy code
val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
scope.launch { /* do blocking stuff */ }
scope.launch { /* do blocking stuff */ }
scope.launch { /* do blocking stuff */ }
a
Oh so great
Just tried it - works great! Thanks man 🙏
z
Don’t forget to cancel the scope appropriately
a
Why would i cancel the scope?
z
To stop those coroutines and avoid leaking them when you don’t need them anymore. If you want them to run as long as your process is alive, you don’t need to cancel it.
a
Hmm. The coroutines are launched - they do some stuff and exit. Do i still have to cancel?
Should be getting cleaned up auto right
z
It’s good “hygiene” to clean up your scope, to prevent leaks (in case, for some reason, one of the coroutines doesn’t actually exit itself, or is still running when your scope is cleaned up)