I’ve got a library to use within ktor which does blocking operations via jdbc. Unfortunately it’s our core library and I cannot get rid of it or make it asynchronous quickly. So I’ll have to call this library and block. Is it still possible for me to still use ktor coroutine structure and maybe schedule the query jobs on a thread pool but then how to I wait for the result all asynchronously through coroutines?
Usually you just invoke blocking operation and it works fine. The problem arises then you have multiple blocking calls running simultaneously. They can exhaust CommonPool and block completely. I see three possibility here:
If operations are fast and you are sure that they won’t block forever, you can call them inside coroutines.
If operations are long, but you have only few of them, you can use custom CoroutineContext with increased number of parallel threads.
If you have a large number of those blocking operations, you should just good old executor. If you want to evaluate the result, you can create a Channel and send results to it.