kotlinx.coroutines
kotlinx.coroutines copied to clipboard
Support "worker pool" pattern in actor builder and other related operators
actor builder should natively support "worker pool" pattern via an additional optional parameter ~parallelism~ concurrency that defaults to 1, so that to you if you have a list of of some requests, then can be all processed concurrently with a specified limited concurrency with a simple code like this:
val reqs: List<Request> = ...
val workers = actor(concurrency = n) {
for (it in channel) processeRequest(it)
}
This particular pattern seems to be quite common, with requests being stored in either a list of requests of receive from some other channel, so the proposal is to add concurrency to map, and cosumeEach, too, to be able to write something like:
incomingRequests.consumeEach(concurrency = n) { processRequest(it) }
UPDATE: We will consistently call it concurrency here. We can have dozens of concurrent coroutines which run on a single CPU core. We will reserve the name parallelism to denote limits on the number of CPU cores that are used.