Need help with what dispatchers i'm supposed to use..
So far I tried:
Copy code
runBlocking {
launch (Dispatchers.Unconfined) {
// check every x seconds if file exists, if it does then kill app (and all other coroutines)
}
launch (<http://Dispatchers.IO|Dispatchers.IO>) {
// every x seconds send metrics to server
}
for (i in 1..Runtime.getRuntime().availableProcessors()) {
launch (Dispatchers.Default) {
// use remaining cpu/mem resources to process batch items
}
}
}
running this means that when all available processors are doing work, the jobs which are supposed to run every x seconds never run, how am I supposed to get them to run regularly regardless of if available processors are performing the batch work?
w
withoutclass
11/07/2019, 5:00 PM
You need to design your coroutine usage to be cooperative
withoutclass
11/07/2019, 5:01 PM
IIRC default dispatchers is already based on CPU availability so you're actually limiting your batch processors there
withoutclass
11/07/2019, 5:01 PM
Also, for your batch work, you'll want to bake in some calls to
yield
or other suspension points that way it give a chance for other coroutines to run
withoutclass
11/07/2019, 5:02 PM
if your batch work is never taking a break you're effectively pegging the cpu and not allowing other work to be done
withoutclass
11/07/2019, 5:02 PM
coroutines are not magic, you must think about threading still