# How to increase ray performance for cpu and io bound operations in a task

**URL:** <https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020>\
**Category:** Ray Core\
**Created:** [July 26, 2021, 5:49pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020 "2021-07-26T17:49:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yraut](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yraut](https://discuss.ray.io/u/yraut)\
**Post date:** [July 26, 2021, 5:49pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/1 "2021-07-26T17:49:58Z")

</div>

Hello Team,  
I need to get data by calling some rest api and then need to perform some operations on it. Api call sometime takes 11 seconds and operations are cpu bound those takes few milliseconds.

Currently I kept everything in task but thinking to split getting data into async actor and computation in task.

I am running this task with single cpu and 1 gb ram.  
Suppose I have to run 1000 time this task concurrently then how should I go .

Does this is correct approach or I am making some mistakes.

---

<div class="post-metadata">

**Author:** ![asm582](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/asm582/32/669_2.png) [@asm582](https://discuss.ray.io/u/asm582)\
**Post date:** [July 27, 2021, 1:32pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/2 "2021-07-27T13:32:16Z")

</div>

are you trying to run tasks concurrently or in a parallel manner? if data remains unchanged have to tried adding data to ray’s object store?

---

<div class="post-metadata">

**Author:** ![yraut](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yraut](https://discuss.ray.io/u/yraut)\
**Post date:** [July 27, 2021, 4:38pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/3 "2021-07-27T16:38:18Z")

</div>

I am putting objects in ray object store only.

---

<div class="post-metadata">

**Author:** ![asm582](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/asm582/32/669_2.png) [@asm582](https://discuss.ray.io/u/asm582)\
**Post date:** [July 27, 2021, 4:54pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/4 "2021-07-27T16:54:48Z")

</div>

thanks, from what I know ray spawns processes to run tasks in a parallel and distributed manner. if you have a cluster.

Sharing a code snippet that may help

#spawn 1000 tasks of your\_func  
tasks = [your\_func for \_ in range(1000)]  
#get results of all 1000 tasks  
print(ray.get(tasks))

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [July 28, 2021, 6:14pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/5 "2021-07-28T18:14:01Z")

</div>

Yeah this is the right way to go. You can have a async actor which calls the API and invoke a task based on the result; example;

```auto
class APIReadActor:
    async def run(self):
        while True:
            data = call_api()
            await ray.get(cpu_task.remote(data))
            await asyncio.sleep(0) # This is a hack to run while loop in an actor while it can still process other requests. Note that the actor is always single threaded, so if you run while loop in one of its tasks, it cannot process other tasks originally.

```

---

<div class="post-metadata">

**Author:** ![ericl](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ericl/32/133_2.png) [@ericl](https://discuss.ray.io/u/ericl)\
**Post date:** [August 3, 2021, 8:38pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/6 "2021-08-03T20:38:32Z")

</div>

Another approach can be to allocate fractional CPUs to tasks, for example:

@ray.remote(num\_cpus=0.1)  
def io\_task():  
pass

This is simpler than the actor approach, but has some limitations in that you cannot reduce the number of CPUs indefinitely small, since each task still needs a worker process.

---

<div class="post-metadata">

**Author:** ![yraut](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yraut](https://discuss.ray.io/u/yraut)\
**Post date:** [August 6, 2021, 8:16am UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/7 "2021-08-06T08:16:12Z")

</div>

Thank you @sangcho . Will publish the result of this… I will be trying making this change and see how this works.

---

<div class="post-metadata">

**Author:** ![yraut](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yraut](https://discuss.ray.io/u/yraut)\
**Post date:** [August 6, 2021, 8:17am UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/8 "2021-08-06T08:17:35Z")

</div>

thank you for reply. this still consume some sort of cpu…

---

<div class="post-metadata">

**Author:** ![yraut](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yraut](https://discuss.ray.io/u/yraut)\
**Post date:** [August 6, 2021, 8:19am UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/9 "2021-08-06T08:19:59Z")

</div>

One quick question.  
**Does each async actor will be executed in separate event loop?..**  
If yes then i can create separate actor for each i/o operations…

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [August 9, 2021, 10:06pm UTC](https://discuss.ray.io/t/how-to-increase-ray-performance-for-cpu-and-io-bound-operations-in-a-task/3020/10 "2021-08-09T22:06:15Z")

</div>

async actor shares the same event loop. Btw, spending some cpus are expected because it still needs to run some code within the actor. Are you saying the cpu consumption is high?
