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

bdawg.io

11/29/2018, 6:47 AM
Is there a way to configure the maximum number of active concurrent coroutines in a CoroutineScope/Context?
g

gildor

11/29/2018, 7:08 AM
No, this is not how context/scope works, at least for now. To limit amount of coroutines use worker pool pattern
BTW what is your use case?
One more problem that how limit number of coroutines, you cannot do this on launch/async call, because those functions are not suspendable, so you cannot limit it like for
Channel.send()
. And coroutines that run non-blocking code also cannot be limited, because they will be dispatched almost instantly, no blocking code there
the only proper way to do that (which I know). Use the worker pool pattern: run N coroutines (your concurrency limitation) and each of those coroutines reads tasks from a channel and suspends when there are no tasks to run
Probably dispatcher could limit amout of coroutines that run concurrently and just do not dispatch other, but this looks as very questionable limitation which can be harmful for many use cases, tricky to manage and maybe even against coroutines ideology
b

bdawg.io

11/29/2018, 6:30 PM
I don’t have a particular use case, more just out of curiosity if the CoroutineDispatcher provided such functionality already or not
4 Views