Home > Software design >  Kotlin coroutines running sequentially even with keyword async
Kotlin coroutines running sequentially even with keyword async

Time:11-11

Hi guys i'm trying to improve performance of some computation in my system. Basically I want to generate a series of actions based on some data. This doesn't scale well and I want to try doing this in parallel and getting a result after (a bit like how futures work)

I have an interface with a series of implementations that get a collection of actions. And want to call all these in parallel and await the results at the end.

The issue is that, when I view the logs its clearly doing this sequentially and waiting on each action getter before going to the next one. I thought the async would do this asynchronously, but its not.

The method the runBlocking is in, is within a spring transaction. Maybe that has something to do with it.

runBlocking {
       val actions = actionsReportGetters.map { actionReportGetter ->
           async {
             getActions(actionReportGetter, abstractUser)
           }
       }.awaitAll().flatten()

       allActions.addAll(actions)
}
private suspend fun getActions(actionReportGetter: ActionReportGetter, traderUser: TraderUser): List<Action> {
        return actionReportGetter.getActions(traderUser)
}
interface ActionReportGetter {

    fun getActions(traderUser: TraderUser): List<Action>
}

CodePudding user response:

Looks like you are doing some blocking operation in ActionReportGetter.getActions in a single threaded environment (probably in the main thread). For such IO operations you should launch your coroutines in Dispatchers.IO which provides a thread pool with multiple threads.

Update your code to this:

async(Dispatchers.IO) { // Switch to IO dispatcher
    getActions(actionReportGetter, abstractUser
}

Also getActions need not be a suspending function here. You can remove the suspend modifier from it.

  • Related