maxmello
12/15/2022, 4:11 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { new { ... } }
I have DB code from Exposed (new
entity creation) that is not suspending and has this parameter: init: T.() -> Unit
, which as you can see does not allow suspension.
If I now really need to call a suspended function, I would usually do so before the new
block and reference the result. Now I thought, what happens if I just call runBlocking
with my suspending code inside new
? Will it “block more” this way, or be the same since the new
block has no suspending capabilities anyway?Sam
12/15/2022, 4:14 PMnew
will take longer. So the thread it borrows from the IO dispatcher will be occupied for more total time.maxmello
12/15/2022, 4:15 PMSam
12/15/2022, 4:16 PMSam
12/15/2022, 4:18 PMmaxmello
12/15/2022, 4:24 PMPatrick Steiger
12/15/2022, 4:24 PMnew
were inline
you would be able to call a suspend fun inside its lambda if you already were in a suspend fun (as the lambda would be inlined)Sam
12/15/2022, 4:25 PMnew
itself end up making a DB call? Because if yes then definitely don’t make another DB call inside of it 😬 that sounds like a recipe for connection pool starvation (with the caveat that I don’t know how Exposed works, maybe it’s actually fine)maxmello
12/15/2022, 4:26 PMSam
12/15/2022, 4:28 PMmaxmello
12/15/2022, 4:32 PMnew
that itself really is just a withContext(Dispatchers.IO) { // blocking code }, does it make it “block more” to use runBlocking
? or would it only be a problem if the suspending function would actually make use of suspension in a proper way (then it makes sense to me that it would block instead of actually suspend)maxmello
12/15/2022, 4:33 PMPatrick Steiger
12/15/2022, 4:37 PMSam
12/15/2022, 4:39 PMrunBlocking
isn’t going to have much impact, but it will add a little bit of overhead for the event loop machinery it sets up. Aside from that overhead, it won’t block the thread any more than the code running within it already does.Sam
12/15/2022, 4:43 PMmaxmello
12/15/2022, 5:10 PMuli
12/15/2022, 10:44 PM