Is there a way to configure the maximum number of ...
# coroutines
b
Is there a way to configure the maximum number of active concurrent coroutines in a CoroutineScope/Context?
g
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
I don’t have a particular use case, more just out of curiosity if the CoroutineDispatcher provided such functionality already or not