# Ray not parallelising function

**URL:** <https://discuss.ray.io/t/ray-not-parallelising-function/6312>\
**Category:** Ray Core\
**Created:** [May 28, 2022, 1:47am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312 "2022-05-28T01:47:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yudhiesh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yudhiesh/32/2622_2.png) [@yudhiesh](https://discuss.ray.io/u/yudhiesh)\
**Post date:** [May 28, 2022, 1:47am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/1 "2022-05-28T01:47:10Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

I have the following function that extracts the text out of HTML files, which I am trying to parallelise:

```auto
def extract_text_from_html(html_files):
    """
    Extract relevant text data from the HTML file
    """
    return [get_html_text(file=file) for file in html_files]

# Convert to remote function
extract_text_from_html_ = ray.remote(extract_text_from_html)

```

I am running it like so:

```auto
ray.init(num_cpus=psutil.cpu_count()) # 12 cores
html_files = load_html_files()
text_ids = extract_text_from_html_.remote(html_files)
texts = ray.get(text_ids)

```

Checking the Ray Dashboard when running the function I notice that only a single process is being run instead of 12, so I am confused why aren’t all the cores being used?

 ![Screenshot 2022-05-28 at 9.41.17 AM](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/c/c0590c1e6ab49fd4b84fff21151ac37bf61159b8.jpeg)

---

<div class="post-metadata">

**Author:** ![yudhiesh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yudhiesh/32/2622_2.png) [@yudhiesh](https://discuss.ray.io/u/yudhiesh)\
**Post date:** [May 28, 2022, 2:18am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/2 "2022-05-28T02:18:01Z")

</div>

I managed to fix the issue, but it seems that it is counterintuitive to what is mentioned in the documentation about [Avoiding Tiny Tasks](https://docs.ray.io/en/latest/ray-core/tips-for-first-time.html#tip-2-avoid-tiny-tasks).  
I had to convert the inner task to a remote function to utilise all the cores. Could I get an explanation about why this is the case?

```auto
html_files = load_html_files()
get_html_text_ = ray.remote(get_html_text)
text_ids = [get_html_text_.remote(file) for file in html_files]
print(ray.get(text_ids))

```

 ![Screenshot 2022-05-28 at 10.13.22 AM](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/e/ec9b93ce3866684dfce4b6bc0d806e4eb15e95c2.jpeg)

Note: `get_html_text()` loads the file and extracts the text out of it.

---

<div class="post-metadata">

**Author:** ![zhz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/zhz/32/12_2.png) [@zhz](https://discuss.ray.io/u/zhz)\
**Post date:** [May 28, 2022, 3:19am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/3 "2022-05-28T03:19:28Z")

</div>

Thanks for the question @yudhiesh

Basically, you should be using an appropriate granularity that is neither too big (like in your original post, just one `Task`, nor too small (like in your second post).

Depending on how long a single `get_html_text` takes, you might want to batch a few of them into a `Task`. E.g. you can break `html_files` into a number of smaller lists.

---

<div class="post-metadata">

**Author:** ![yudhiesh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yudhiesh/32/2622_2.png) [@yudhiesh](https://discuss.ray.io/u/yudhiesh)\
**Post date:** [May 28, 2022, 6:19am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/4 "2022-05-28T06:19:45Z")

</div>

Thank you, yes batching the files solved it but I needed to run it on `extract_text_from_html()` instead.

```auto

def chunk_list(elements, batch_size):
    for i in range(0, len(elements), batch_size):
        yield elements[i : i + batch_size]

# Convert the function that handled a list of files into a remote function
extract_text_from_html_ = ray.remote(extract_text_from_html)

html_files = [file for file in chunk_list(load_html_files(), batch_size=100)]
text_ids = [extract_text_from_html_.remote(file) for file in html_files]
print(ray.get(text_ids))

```

All cores are being utilised now. Timings for both approaches were almost identical.

 ![Screenshot 2022-05-28 at 2.16.17 PM](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/5/51ace1c4708b82defdedc24894a0d1129526c2c9.jpeg)

---

<div class="post-metadata">

**Author:** ![rliaw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rliaw/32/24_2.png) [@rliaw](https://discuss.ray.io/u/rliaw)\
**Post date:** [June 2, 2022, 9:26pm UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/5 "2022-06-02T21:26:54Z")

</div>

> [@yudhiesh](#):
>
> I had to convert the inner task to a remote function to utilise all the cores. Could I get an explanation about why this is the case?

It seems like the fundamental confusion is as to what `ray.remote` does. It does not automatically “parallelize” your function call. Instead, it runs the function asynchronously on a background process.

If you want a “parallel application” of the function, you will have to launch multiple of these functions asynchronously:

```python
remote_func = ray.remote(func)
remote_func.remote() ## runs func once as a background task

[remote_func.remote() for i in range(100)] # runs func as 100 background task

```

Does that make sense? Let me know if I captured your confusion and question properly.

---

<div class="post-metadata">

**Author:** ![yudhiesh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yudhiesh/32/2622_2.png) [@yudhiesh](https://discuss.ray.io/u/yudhiesh)\
**Post date:** [June 3, 2022, 1:00am UTC](https://discuss.ray.io/t/ray-not-parallelising-function/6312/6 "2022-06-03T01:00:28Z")

</div>

Thanks for clearing up the confusion, yes that answers it.
