# ktor

Owain Evans

03/25/2021, 1:36 AM
Hi - I'm new to ktor but have used coroutines in Android before. And I'm concerned I'm that I'm over engineering my code due to my lack of understanding of coroutines. My understanding of coroutines is as follows: • Coroutine jobs run on a context. • Each context is associated with a thread or pool of threads which can be allocated a priority. • Many jobs can efficiently run on the same context, even if there is actually only one thread, with jobs constantly being suspended and interleaved. • You are still limited to the hardware you are running on, so the only advantage of having multiple contexts are to manage priority and allow jobs to be stopped/tidied up if the process that spawned the job has stopped etc. In Android it's easy to understand that you need to get many long running processes off the main thread, and when we started doing that we noticed that the UI experience of the app was noticeable improved, smother scrolling etc. With my ktor application I'm not too sure what I'm doing. My understanding and looking at the source code suggests that when my
function is called I'm already on a coroutine context. My questions are: • Do I need to ever move away from the default context that ktor server call starts on? - I don't think I'm doing anything computationally expensive (I think), but I am wrapping some third party callbacks like firestore and calling a few third party api's which I'm using the ktor client for anyway. • Are there tools to monitor and discover what parts of the code I should be moving to a different, lower priority, context?


03/29/2021, 5:04 AM
Unless you have long running process, you shouldn't need to.