# Divide Work between Actors

**URL:** <https://discuss.ray.io/t/divide-work-between-actors/498>\
**Category:** Ray Core\
**Created:** [January 20, 2021, 4:48pm UTC](https://discuss.ray.io/t/divide-work-between-actors/498 "2021-01-20T16:48:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lemar94](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lemar94/32/273_2.png) [@lemar94](https://discuss.ray.io/u/lemar94)\
**Post date:** [January 20, 2021, 4:48pm UTC](https://discuss.ray.io/t/divide-work-between-actors/498/1 "2021-01-20T16:48:29Z")

</div>

My application has a Actor class that process incoming data. In order to do so the class itself holds a large amount of parameters that are supplied via the constructor.  
The current approach creates one Actor per data package, to process which creates a large number of actors (\>500) and therefore lots of overhead.

So my question is: Is their an easy way to have a pool of N actors and auto-distribute the M data packages (M \>\> N) between these actors?

Thank you in advance for your answers.

---

<div class="post-metadata">

**Author:** ![kai](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/kai/32/3380_2.png) [@kai](https://discuss.ray.io/u/kai)\
**Post date:** [January 20, 2021, 4:56pm UTC](https://discuss.ray.io/t/divide-work-between-actors/498/2 "2021-01-20T16:56:35Z")

</div>

Hi lemar, this really depends on how you structured data ingestion. Theoretically it shouldn’t be a problem at all; you can just create `N` actors and pass the data bundle with index `i` to the actor with index `i % N`.

Something like this:

```auto
N = 50
actors = [Actor.remote() for i in range(N)

for i, m in enumerate(get_data()):
    actors[i % N].process.remote(m)

```

This is a simple example for load balancing. Does this help already or are you looking for something more specific?

---

<div class="post-metadata">

**Author:** ![lemar94](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lemar94/32/273_2.png) [@lemar94](https://discuss.ray.io/u/lemar94)\
**Post date:** [January 20, 2021, 5:28pm UTC](https://discuss.ray.io/t/divide-work-between-actors/498/3 "2021-01-20T17:28:10Z")

</div>

Hi kai,

thank you for your answer.  
I think this should suffice for my application, as the data packages have approximately the same size and are all availabe as soon as i start the application.  
Their is still one question i have with your suggestion:  
How would i get back the processed data? Can I iterate over all actors and call ray.get() on each of them and get back the results for all data packages send to the actor?

---

<div class="post-metadata">

**Author:** ![lemar94](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lemar94/32/273_2.png) [@lemar94](https://discuss.ray.io/u/lemar94)\
**Post date:** [January 21, 2021, 10:33am UTC](https://discuss.ray.io/t/divide-work-between-actors/498/4 "2021-01-21T10:33:49Z")

</div>

Based on Kais answer i was able to create a minimum running example that suffices my use case. It seems the solution was much easier than i thought originally. Here is the complete code of my solution:

```
import ray

@ray.remote
class Actor:
    def __init__ (self, params):
        self.params = params

    @ray.method(num_returns= 1)
    def calculate(self, input):
        return self.params[0] * input

if __name__ == ' __main__':
    ray.init()
    # create parameters
    params = (10,)
    # create example input values
    inputData = [cnt*0.1 for cnt in range(1000)]

    # create a list of actors
    numberOfWorkers = 10
    actorList = [Actor.remote(params) for i in range(numberOfWorkers)]

    # balance load between actors
    actorResults = list()
    for i, data in enumerate(inputData):
        actorResults.append(actorList[i % numberOfWorkers].calculate.remote(data))
    output = [ray.get(result) for result in actorResults]
```

---

<div class="post-metadata">

**Author:** ![kai](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/kai/32/3380_2.png) [@kai](https://discuss.ray.io/u/kai)\
**Post date:** [January 21, 2021, 8:13pm UTC](https://discuss.ray.io/t/divide-work-between-actors/498/5 "2021-01-21T20:13:03Z")

</div>

Hi @lemar94, thanks for sharing your complete solution!

One thing to add, you might want to call

```auto
output = ray.get(actorResults)

```

instead. This will automatically pull all results and return a list. Generally it’s easier to iterate from this (e.g. using `ray.wait()`) to process results as they come in eventually.

---

<div class="post-metadata">

**Author:** ![Alex](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/alex/32/341_2.png) [@Alex](https://discuss.ray.io/u/Alex)\
**Post date:** [January 22, 2021, 1:38am UTC](https://discuss.ray.io/t/divide-work-between-actors/498/6 "2021-01-22T01:38:39Z")

</div>

Hey, looks like you already found a solution, but I just wanted to chime in (if only for posterity) that it sounds like the behavior you’re describing matches Ray’s ActorPool. [Using Actors — Ray v2.0.0.dev0](https://docs.ray.io/en/master/actors.html#actor-pool)
