question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support "worker pool" pattern in actor builder and other related operators

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:37
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

16reactions
gildorcommented, Oct 23, 2018

I want to bump this issue. This pattern is so often, I see questions about implementation at least each week on Kotlin Slack #coroutines channel also all fast ad-hoc implementations often have problems (a similar problem we had before awaitAll extensions, when simple extension functions just use map { it.await() } which leak coroutines in case of error)

8reactions
rnettcommented, Jan 27, 2019

This pattern is common enough even outside of actors (e.g. make a lot of web requests, but only have 10 going at a time) that it seems like it might be worth having a separate api for launching n amount of coroutines, and use that here, rather than vise versa. At the very least there should be something similar for produce.

Something like:

coroutineScope{
    limitedConcurrency(concurrency = 10){
        (1..100).forEach{
            launch{ doThing() }
        }
    }
}

Only 10 doThings would be executing at any given time.

Where any launches would be redirected to either a worker thread, forced to be lazy and started once there is room, or just have the block held until there is room, then launched.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Actors — Ray 2.2.0
Actors extend the Ray API from functions (tasks) to classes. An actor is essentially a stateful worker (or a service). When a new...
Read more >
5.4 Actor-based Concurrency
Event-driven Actors​​ Instead, a thread pool can be used for a number of actors. This approach uses a continuation closure to encapsulate the...
Read more >
How the Actor Model Meets the Needs of Modern, Distributed ...
An execution environment orchestrates a pool of threads to drive all these actions completely transparently.
Read more >
Design Principles and Patterns for Highly Concurrent ...
Thread : A thread, on the other hand, is just a segment of a process. ... However, we execute actors over a thread...
Read more >
Theoretical Computer Science Scala Actors: Unifying thread ...
Event-based actors support the same operations as thread-based actors, ... Upon timeout the action associated with a special TIMEOUT pattern is fired.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found