https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

IRomanov

08/30/2018, 9:49 PM
Hello. I want to use
async
coroutine builder to execute some blocking code in parallel. I use my custom thread pool for that. But is it ok to call regular (non-suspending) function inside
async
?
Copy code
fun doSomething(s: String): String {
// blocking code
}

val def1 = async(myContext) { doSomething("a") }
val def2 = async(myContext) { doSomething("b") }
// ...

println("${def1.await()} ${def2.await()}")
r

rocketraman

08/30/2018, 9:51 PM
I think you should use the new
IO
dispatcher for the non-suspending blocking code.
👍 2
i

IRomanov

08/30/2018, 10:14 PM
Thank you. I will try it.
u

uli

08/31/2018, 7:02 AM
@rocketraman are your 'regular' functions blocking or computing? If they are cumputationally intensive the default limit of up to 64 threads might be a little high.
r

rocketraman

08/31/2018, 8:23 AM
I assumed they were blocking as that was what the OP stated. Agreed, if they are computing, there would be no benefit to using the
IO
pool.
u

uli

08/31/2018, 8:52 AM
sorry, wrong mention. I meant @IRomanov
3 Views