# Question about multiprocessing large array using ray.remote

**URL:** <https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083>\
**Category:** Ray Core\
**Created:** [May 9, 2022, 7:46pm UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083 "2022-05-09T19:46:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qian\_Huang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/qian_huang/32/2504_2.png) [@Qian\_Huang](https://discuss.ray.io/u/Qian_Huang)\
**Post date:** [May 9, 2022, 7:46pm UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083/1 "2022-05-09T19:46:46Z")

</div>

Hi all,

I am looking for an efficient way to multiprocess a large array, which only needs to be read-only for each thread. I found this solution using ray: [python - python3 multiprocess shared numpy array(read-only) - Stack Overflow](https://stackoverflow.com/questions/54580947/python3-multiprocess-shared-numpy-arrayread-only)

However, when I try this method out, the data in the worker\_func remains an ObjectRef and throws errors when I try to process it. Here is a minimal example of what I hope to get work

```auto
import ray
import ray.util.multiprocessing as mp
import torch

@ray.remote
def f(args_):
    idx, A_= args_
    return A_[idx]
    

    
ray.init()
A = torch.rand(5, 5)
A_ = ray.put(A)

with mp.Pool() as p:
    args_ = zip(range(5), [A_] *5)
    results = p.map(f.remote, args_)
    print(results = ray.get(results))

```

```auto
TypeError: 'ray._raylet.ObjectRef' object is not subscriptable

```

Very new to ray, any pointers are appreciated, thanks!

---

<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:** [May 10, 2022, 3:59am UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083/2 "2022-05-10T03:59:32Z")

</div>

Hey @Qian_Huang, that error means that `A_` is an ObjectRef. This happens because Ray will not dereference refs automatically unless they are passed as top-level arguments. But you can de-reference it by using `ray.get(A_)`:

```auto
import ray
import ray.util.multiprocessing as mp
import torch

@ray.remote
def f(args_):
    idx, A_= args_
    A_ = ray.get(A_) # <---- added ray.get()
    return A_[idx]
    

A = torch.rand(5, 5)
A_ = ray.put(A)

with mp.Pool() as p:
    args_ = zip(range(5), [A_] *5)
    results = p.map(f.remote, args_)
    print(ray.get(results))

```

You can read more about Ray objects here: [Objects — Ray 1.12.0](https://docs.ray.io/en/latest/ray-core/objects.html)

---

<div class="post-metadata">

**Author:** ![Qian\_Huang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/qian_huang/32/2504_2.png) [@Qian\_Huang](https://discuss.ray.io/u/Qian_Huang)\
**Post date:** [May 10, 2022, 5:29am UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083/3 "2022-05-10T05:29:50Z")

</div>

Hi @ericl Thanks for helping!

Another thing I am trying to figure out is how to use tqdm with this to show progress? For normal pool I can use imap, but seems imap behaves differently in ray: ray seems just sends the entire args\_ to f

```auto
import ray
import ray.util.multiprocessing as mp
import torch
from tqdm import tqdm
import ray
import ray.util.multiprocessing as mp
import torch
import time

def f(args):
    idx, A_ = args
    time.sleep(idx)
    return ray.get(A_)[idx]
    

    
ray.init()
A = torch.rand(5, 5)
A_ = ray.put(A)

pool = mp.Pool()
args_ = zip(range(5), [A_] *5)

for result in tqdm(pool.imap(f, args_), total=5):
    print(result)

```

```auto
too many values to unpack (expected 2)

```

Thanks!

---

<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:** [May 10, 2022, 6:11am UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083/4 "2022-05-10T06:11:17Z")

</div>

Huh, that’s a strange. Possibly a bug in ray.multiprocessing (cc @eoakes ). As a workaround, you could avoid passing A\_ as an arg and closure capture it as a global variable:

```auto
from tqdm import tqdm
import ray
import ray.util.multiprocessing as mp
import torch
import time

    
ray.init()
A = torch.rand(5, 5)
A_ = ray.put(A)

def f(idx):
    time.sleep(idx)
    return ray.get(A_)[idx]

pool = mp.Pool()
args_ = range(5)

for result in tqdm(pool.imap(f, args_), total=5):
    print(result)

```

---

<div class="post-metadata">

**Author:** ![Qian\_Huang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/qian_huang/32/2504_2.png) [@Qian\_Huang](https://discuss.ray.io/u/Qian_Huang)\
**Post date:** [May 10, 2022, 6:18am UTC](https://discuss.ray.io/t/question-about-multiprocessing-large-array-using-ray-remote/6083/5 "2022-05-10T06:18:33Z")

</div>

Thanks for the quick response!

Yeah but I happen to need to pass in a lot of other arguments lol I think this is probably related to [[util.multiprocessing] Support generators · Issue #9712 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/9712). As a workaround, I just converted the zip to a list first and that seems to work well.
