Riku
12/29/2019, 8:23 AMtseisel
12/29/2019, 11:12 AMDispatchers.Main
may not exist on the server-side.
• Dispatchers.Default
should be used for computational tasks that requires CPU power such as reducing, transforming, etc. It has a pool of thread equal to the number of CPU cores to avoid overloading the CPU when scheduling too much work.
• <http://Dispatchers.IO|Dispatchers.IO>
, as its name suggests, should be used for blocking IO such as database access, file read/write and network communication. It has a larger pool of threads than Default
because IO tasks don't need much CPU time, it just waits and blocks a thread.Riku
12/29/2019, 12:30 PMtseisel
12/29/2019, 3:03 PM<http://Dispatchers.IO|Dispatchers.IO>
since it has the largest thread pool and your task requires some network connectivity.
You could write it this way:
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
while(true) {
postStatusUpate(...)
delay(10 * 60 * 1000) // 10 minutes in milliseconds
}
}
Note that while the coroutine is suspended in delay
it is not actively using a thread.Riku
12/29/2019, 4:01 PM