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

gotoOla

06/17/2019, 3:18 PM
What are peoples goto solution for confining number of parallel coroutines? If I use a dispatcher with e.g. 20 threads I guess I can still executon infinite number of coroutines to execute on these threads?
does there exist any such support or do you have to “implement” something else to keep track of this via channels/references to jobs/deffered values?
e

elizarov

06/17/2019, 3:21 PM
What exactly are you trying to do?
g

gotoOla

06/17/2019, 3:28 PM
@elizarov so I have something that reads from a queue, so I want to pick up a message -> fire off business logic (non-blocking)
so that the next message can be picked up without having to wait for the first message to be fully processed, however, I want max 100 messages to be processed at one time
e

elizarov

06/17/2019, 3:31 PM
There are two approaches: 1. Use “worker pool” pattern — create 25 coroutines and a channel ahead of time. Have all coroutines receive from the channel (fan-out) and execute task, send work to the channel when needed. 2. Just launch a new coroutine for each new piece of work, but use use semaphore to limit concurrency (acquire permit from semaphore before launching a coroutine)
g

gotoOla

06/17/2019, 3:33 PM
Ah ok I see. I guess the worker-pool pattern would make more sense to me just because I am familiar with that behavior before 🙂 And the overhead of 25 “in memory” coroutines is not that much of a trade off. Thank you!
HI @elizarov, I had to leave but I finally sat down again and did a little test, what I have now is this:
Copy code
suspend fun main() {
    val channel = Channel<Job>(5)

    GlobalScope.launch {
        while(true) {
            channel.send(doJob(System.currentTimeMillis()))
        }

    }
    for(job in channel) {
        job.join()
    }

}

private fun doJob(time: Long): Job {
    return GlobalScope.launch {
        println("starting a job and waiting")
        delay(5000)
    }
}
But I see the logs come in bursts of 7 even though the capacity is 5. Did I missunderstand something here?
e

elizarov

06/17/2019, 8:49 PM
Burst size is always capacity + 2
i had it explained somewhere…
g

gotoOla

06/17/2019, 8:55 PM
Okok! I might have to sit down and watch your talk from the latest KotlinConf again, watched it when it was released but we haven’t worked with channels until now
Anyway I think this would work as a quite nice fan out strategy when reading our messages, thanks for the pointers!